<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
    <title>Moses documentation</title>
    <link rel="stylesheet" href="../ldoc_fixed.css" type="text/css" />
</head>
<body>

<div id="container">

<div id="product">
	<div id="product_logo"></div>
	<div id="product_name"><big><b></b></big></div>
	<div id="product_description"></div>
</div> <!-- id="product" -->


<div id="main">


<!-- Menu -->

<div id="navigation">
<br/>
<h1>Moses</h1>


<h2>Contents</h2>
<ul>
<li><a href="#_a_name__table__Table_functions__a_"><a name='table'>Table functions</a> </a></li>
<li><a href="#_a_name__array__Array_functions__a_"><a name='array'>Array functions</a> </a></li>
<li><a href="#_a_name__utility__Utility_functions__a_"><a name='utility'>Utility functions</a> </a></li>
<li><a href="#_a_name__object__Object_functions__a_"><a name='object'>Object functions</a> </a></li>
<li><a href="#_a_name__chaining__Chaining__a_"><a name='chaining'>Chaining</a> </a></li>
<li><a href="#_a_name__import__Import__a_"><a name='import'>Import</a> </a></li>
</ul>


<h2>Manual</h2>
<ul class="nowrap">
  <li><strong>tutorial</strong></li>
</ul>
<h2>Modules</h2>
<ul class="nowrap">
  <li><a href="index.html">moses</a></li>
</ul>

</div>

<div id="content">

    <em>Moses: a utility-belt library for functional programming in Lua</em></p>

<p><strong>Moses</strong> is a Lua utility library which provides support for functional programming.
It complements built-in Lua functions, making easier common operations on tables, arrays, lists, collections, objects, and a lot more.
<br/>
<br/></p>

<h1><a name='TOC'>Sections</a></h1>


<ul>
    <li><a href="#adding">Adding <em>Moses</em> to your project</a></li>
    <li><a href="#table">Table functions</a></li>
    <li><a href="#array">Array functions</a></li>
    <li><a href="#utility">Utility functions</a></li>
    <li><a href="#object">Object functions</a></li>
    <li><a href="#chaining">Chaining</a></li>
    <li><a href="#import">Import</a>
    <br/><br/>
    <h1><a name='adding'>Adding <em>Moses</em> to your project</a></h1></li>
</ul>

<p>Drop the file <a href="http://github.com/Yonaba/Moses/blob/master/moses.lua">moses.lua</a> into your project and add it to your code with the <em>require</em> function:</p>


<pre>
<span class="keyword">local</span> M = <span class="global">require</span> (<span class="string">"moses"</span>)
</pre>


<p><em>Moses</em> provides a large set of functions that can be classified into four categories:</p>

<ul>
    <li><a href="#table"><strong>Table functions</strong></a>, which are mostly meant for tables, i.e Lua tables which contains both an array-part and a hash-part,</li>
    <li><a href="#array"><strong>Array functions</strong></a>, meant for array lists (or sequences),</li>
    <li><a href="#utility"><strong>Utility functions</strong></a>,</li>
    <li><a href="#object"><strong>Object functions</strong></a>.</li>
</ul>

<p><strong><a href="#TOC">[⬆]</a></strong></p>

<p><a name="_a_name__table__Table_functions__a_"></a></p>
<h2><a name='table'>Table functions</a></h2>

<h3>clear (t)</h3>

<p>Clears a table. All its values becomes nil. Returns the passed-in table.</p>


<pre>
M.clear({<span class="number">1</span>,<span class="number">2</span>,<span class="string">'hello'</span>,<span class="keyword">true</span>}) <span class="comment">-- =&gt; {}</span>
</pre>


<h3>each (t, f)</h3>
<p>*Aliases: <code>forEach</code>*.</p>

<p>Iterates over each value-key pair in the passed-in table.</p>


<pre>
M.each({<span class="number">4</span>,<span class="number">2</span>,<span class="number">1</span>},<span class="global">print</span>)

<span class="comment">-- =&gt; 4 1
</span><span class="comment">-- =&gt; 2 2
</span><span class="comment">-- =&gt; 1 3</span>
</pre>


<p>The table can be map-like (both array part and hash part).</p>


<pre>
M.each({one = <span class="number">1</span>, two = <span class="number">2</span>, three = <span class="number">3</span>},<span class="global">print</span>)

<span class="comment">-- =&gt; 1 one
</span><span class="comment">-- =&gt; 2 two
</span><span class="comment">-- =&gt; 3 three</span>
</pre>


<p>Can index and assign in an outer table or in the passed-in table:</p>


<pre>
t = {<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>}
M.each(t,<span class="keyword">function</span>(v,i)
  t[i] = v:rep(<span class="number">2</span>)
  <span class="global">print</span>(t[i])
<span class="keyword">end</span>)

<span class="comment">-- =&gt; aa
</span><span class="comment">-- =&gt; bb
</span><span class="comment">-- =&gt; cc</span>
</pre>


<h3>eachi (t, f)</h3>
<p>*Aliases: <code>forEachi</code>*.</p>

<p>Iterates only on integer keys in an array table. It returns value-key pairs.</p>


<pre>
M.eachi({<span class="number">4</span>,<span class="number">2</span>,<span class="number">1</span>},<span class="global">print</span>)

<span class="comment">-- =&gt; 4 1
</span><span class="comment">-- =&gt; 2 2
</span><span class="comment">-- =&gt; 1 3</span>
</pre>


<p>The given array can be sparse, or even have a hash-like part.</p>


<pre>
<span class="keyword">local</span> t = {a = <span class="number">1</span>, b = <span class="number">2</span>, [<span class="number">0</span>] = <span class="number">1</span>, [-<span class="number">1</span>] = <span class="number">6</span>, <span class="number">3</span>, x = <span class="number">4</span>, <span class="number">5</span>}
M.eachi(t,<span class="global">print</span>)

<span class="comment">-- =&gt; 6 -1
</span><span class="comment">-- =&gt; 1 0
</span><span class="comment">-- =&gt; 3 1
</span><span class="comment">-- =&gt; 5 2</span>
</pre>


<h3>at (t, ...)</h3>

<p>Collects values at given keys and returns them in an array.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>}
M.at(t,<span class="number">1</span>,<span class="number">3</span>) <span class="comment">-- =&gt; "{4,6}"
</span>
<span class="keyword">local</span> t = {a = <span class="number">4</span>, bb = <span class="keyword">true</span>, ccc = <span class="keyword">false</span>}
M.at(t,<span class="string">'a'</span>, <span class="string">'ccc'</span>) <span class="comment">-- =&gt; "{4, false}"</span>
</pre>


<h3>adjust (t, key, f)</h3>

<p>Adjusts the value at a given key using a function or a value. In case <code>f</code> is a function, it should be prototyped <code>f(v)</code>.
It does not mutate the given table, but rather returns a new array.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}
M.adjust(t, <span class="number">2</span>, <span class="global">math</span>.sin) <span class="comment">-- =&gt; {1, 0.90929, 3}
</span>
<span class="keyword">local</span> v = {x = <span class="number">1</span>}
 M.adjust(t, <span class="string">'x'</span>, <span class="number">4</span>) <span class="comment">-- =&gt; {x = 4}</span>
</pre>


<p>In case the given <code>key</code> does not exist in <code>t</code>, it throws an error.</p>

<h3>count (t [, val])</h3>

<p>Counts the number of occurences of a given value in a table.</p>


<pre>
M.count({<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>},<span class="number">1</span>) <span class="comment">-- =&gt; 2
</span>M.count({<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>},<span class="number">2</span>) <span class="comment">-- =&gt; 3
</span>M.count({<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>},<span class="number">3</span>) <span class="comment">-- =&gt; 4
</span>M.count({<span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">true</span>},<span class="keyword">false</span>) <span class="comment">-- =&gt; 2
</span>M.count({<span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">true</span>},<span class="keyword">true</span>) <span class="comment">-- =&gt; 1</span>
</pre>


<p>Returns the size of the list in case no value was provided.</p>


<pre>
M.count({<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">3</span>}) <span class="comment">-- =&gt; 5</span>
</pre>


<h3>countf (t, f)</h3>

<p>Counts the number of values passing an iterator test.</p>


<pre>
M.countf({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>}, <span class="keyword">function</span>(v)
  <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span>
<span class="keyword">end</span>) <span class="comment">-- =&gt; 3
</span>
M.countf({<span class="global">print</span>, <span class="global">pairs</span>, <span class="global">os</span>, <span class="global">assert</span>, <span class="global">ipairs</span>}, <span class="keyword">function</span>(v)
  <span class="keyword">return</span> <span class="global">type</span>(v)==<span class="string">'function'</span>
<span class="keyword">end</span>) <span class="comment">-- =&gt; 4</span>
</pre>


<h3>allEqual (t [, comp])</h3>
<p>*Aliases: <code>alleq</code>*.</p>

<p>Checks if all values in a collection are equal. Uses <code>M.isEqual</code> by default to compare values.</p>


<pre>
M.allEqual({<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>}, comp) <span class="comment">-- =&gt; true
</span>M.allEqual({<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">1</span>}, comp) <span class="comment">-- =&gt; false
</span>
<span class="keyword">local</span> t1 = {<span class="number">1</span>, <span class="number">2</span>, {<span class="number">3</span>}}
<span class="keyword">local</span> t2 = {<span class="number">1</span>, <span class="number">2</span>, {<span class="number">3</span>}}
M.allEqual({t1, t2}) <span class="comment">-- =&gt; true</span>
</pre>


<p>Can take an optional <code>comp</code> function which will be used to compare values.</p>


<pre>
<span class="keyword">local</span> t1 = {x = <span class="number">1</span>, y = <span class="number">0</span>}
<span class="keyword">local</span> t2 = {x = <span class="number">1</span>, y = <span class="number">0</span>}
<span class="keyword">local</span> t3 = {x = <span class="number">1</span>, y = <span class="number">2</span>}
<span class="keyword">local</span> t4 = {x = <span class="number">1</span>, y = <span class="number">2</span>}
<span class="keyword">local</span> <span class="keyword">function</span> compx(a, b) <span class="keyword">return</span> a.x == b.x <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> compy(a, b) <span class="keyword">return</span> a.y == b.y <span class="keyword">end</span>

M.allEqual({t1, t2}, compx) <span class="comment">-- =&gt; true
</span>M.allEqual({t1, t2}, compy) <span class="comment">-- =&gt; true
</span>M.allEqual({t3, t4}, compx) <span class="comment">-- =&gt; true
</span>M.allEqual({t3, t4}, compy) <span class="comment">-- =&gt; true
</span>M.allEqual({t1, t2, t3, t4}, compx) <span class="comment">-- =&gt; true
</span>M.allEqual({t1, t2, t3, t4}, compy) <span class="comment">-- =&gt; false</span>
</pre>


<h3>cycle (t [, n = 1])</h3>
<p>*Aliases: <code>loop</code>*.</p>

<p>Returns a function which iterates on each value-key pair in a given table (similarly to <code>M.each</code>), except that it restarts iterating again <code>n</code> times.
If <code>n</code> is not provided, it defaults to 1.</p>


<pre>
<span class="keyword">local</span> t = {<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>}
<span class="keyword">for</span> v <span class="keyword">in</span> M.cycle(t, <span class="number">2</span>) <span class="keyword">do</span>
  <span class="global">print</span>(v)
<span class="keyword">end</span>

<span class="comment">-- =&gt; 'a'
</span><span class="comment">-- =&gt; 'b'
</span><span class="comment">-- =&gt; 'c'
</span><span class="comment">-- =&gt; 'a'
</span><span class="comment">-- =&gt; 'b'
</span><span class="comment">-- =&gt; 'c'</span>
</pre>


<p>Supports array-like tables and map-like tables.</p>


<pre>
<span class="keyword">local</span> t = {x = <span class="number">1</span>, y = <span class="number">2</span>, z = <span class="number">3</span>}
<span class="keyword">for</span> v <span class="keyword">in</span> M.cycle(t) <span class="keyword">do</span>
  <span class="global">print</span>(v)
<span class="keyword">end</span>

<span class="comment">-- =&gt; 2
</span><span class="comment">-- =&gt; 1
</span><span class="comment">-- =&gt; 3</span>
</pre>


<h3>map (t, f)</h3>
<p>*Aliases: <code>collect</code>*.</p>

<p>Executes a function on each value in a given array.</p>


<pre>
M.map({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>},<span class="keyword">function</span>(v)
  <span class="keyword">return</span> v+<span class="number">10</span>
<span class="keyword">end</span>) <span class="comment">-- =&gt; "{11,12,13}"</span>
</pre>



<pre>
M.map({a = <span class="number">1</span>, b = <span class="number">2</span>},<span class="keyword">function</span>(v, k)
  <span class="keyword">return</span> k..v
<span class="keyword">end</span>) <span class="comment">-- =&gt; "{a = 'a1', b = 'b2'}"</span>
</pre>


<p>It also maps both keys and values.</p>


<pre>
M.map({a = <span class="number">1</span>, b = <span class="number">2</span>},<span class="keyword">function</span>(v, k)
  <span class="keyword">return</span> k..k, v*<span class="number">2</span>
<span class="keyword">end</span>) <span class="comment">-- =&gt; "{aa = 2, bb = 4}"</span>
</pre>


<h3>mapi (t, f)</h3>

<p>Executes a function on each value in a given array.</p>


<pre>
M.mapi({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>},<span class="keyword">function</span>(v)
  <span class="keyword">return</span> v+<span class="number">10</span>
<span class="keyword">end</span>) <span class="comment">-- =&gt; "{11,12,13}"</span>
</pre>


<p>It only works for the array-part of the given table.</p>


<pre>
M.map({a = <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>},<span class="keyword">function</span>(v, k)
  <span class="keyword">return</span> k..v
<span class="keyword">end</span>) <span class="comment">-- =&gt; "{'12','23','34','45'}"</span>
</pre>


<h3>reduce (t, f [, state = next(t)])</h3>
<p>*Aliases: <code>inject</code>, <code>foldl</code>*.</p>

<p>Can sum all values in a table. In case <code>state</code> is not provided, it defaults to the first value in the given table <code>t</code>.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> add(a,b) <span class="keyword">return</span> a+b <span class="keyword">end</span>
M.reduce({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>},add) <span class="comment">-- =&gt; 10</span>
</pre>


<p>Or concatenates all values.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> concat(a,b) <span class="keyword">return</span> a..b <span class="keyword">end</span>
M.reduce({<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>},concat) <span class="comment">-- =&gt; abcd   </span>
</pre>


<h3>best (t, f)</h3>

<p>Returns the best value passing a selector function. Acts as a special case of <a href="index.html#reduce">reduce</a>, using the first value in <code>t</code> as
an initial state. It thens folds the given table, testing each of its values <code>v</code> and selecting the value passing the
call <code>f(state,v)</code> every time.</p>


<pre>
<span class="keyword">local</span> words = {<span class="string">'Lua'</span>, <span class="string">'Programming'</span>, <span class="string">'Language'</span>}
M.best(words, <span class="keyword">function</span>(a,b) <span class="keyword">return</span> #a &gt; #b <span class="keyword">end</span>) <span class="comment">-- =&gt; 'Programming'
</span>M.best(words, <span class="keyword">function</span>(a,b) <span class="keyword">return</span> #a &lt; #b <span class="keyword">end</span>) <span class="comment">-- =&gt; 'Lua'</span>
</pre>


<h3>reduceBy (t, f, pred [, state = next(t)])</h3>

<p>Reduces a table considering only values matching a predicate.
For example,let us define a set of values.</p>


<pre>
<span class="keyword">local</span> val = {-<span class="number">1</span>, <span class="number">8</span>, <span class="number">0</span>, -<span class="number">6</span>, <span class="number">3</span>, -<span class="number">1</span>, <span class="number">7</span>, <span class="number">1</span>, -<span class="number">9</span>}
</pre>


<p>And a reduction function which will add up values.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> add(a,b) <span class="keyword">return</span> a+b <span class="keyword">end</span>
</pre>


<p>We can also define some predicate functions.</p>


<pre>
<span class="comment">-- predicate for negative values
</span><span class="keyword">local</span> <span class="keyword">function</span> neg(v) <span class="keyword">return</span> v&lt;=<span class="number">0</span> <span class="keyword">end</span>

<span class="comment">-- predicate for positive values
</span><span class="keyword">local</span> <span class="keyword">function</span> pos(v) <span class="keyword">return</span> v&gt;=<span class="number">0</span> <span class="keyword">end</span>
</pre>


<p>Then we can perform reduction considering only negative or positive values :</p>


<pre>
M.reduceBy(val, add, neg) <span class="comment">-- =&gt; -17
</span>M.reduceBy(val, add, pos) <span class="comment">-- =&gt; 19</span>
</pre>


<p>An initial state can be passed in.</p>


<pre>
M.reduceBy(val, add, neg, <span class="number">17</span>) <span class="comment">-- =&gt; 0
</span>M.reduceBy(val, add, pos, -<span class="number">19</span>) <span class="comment">-- =&gt; 0</span>
</pre>


<h3>reduceRight (t, f [, state = next(t)])</h3>
<p>*Aliases: <code>injectr</code>, <code>foldr</code>*.</p>

<p>Similar to <code>M.reduce</code>, but performs from right to left.</p>


<pre>
<span class="keyword">local</span> initial_state = <span class="number">256</span>
<span class="keyword">local</span> <span class="keyword">function</span> div(a,b) <span class="keyword">return</span> a/b <span class="keyword">end</span>
M.reduceRight({<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">16</span>},div,initial_state) <span class="comment">-- =&gt; 2</span>
</pre>


<h3>mapReduce (t, f [, state = next(t)])</h3>
<p>*Aliases: <code>mapr</code>*.</p>

<p>Reduces while saving intermediate states.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> concat(a,b) <span class="keyword">return</span> a..b <span class="keyword">end</span>
M.mapReduce({<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>},concat) <span class="comment">-- =&gt; "{'a', 'ab', 'abc'}"</span>
</pre>


<h3>mapReduceRight (t, f [, state = next(t)])</h3>
<p>*Aliases: <code>maprr</code>*.</p>

<p>Reduces from right to left, while saving intermediate states.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> concat(a,b) <span class="keyword">return</span> a..b <span class="keyword">end</span>
M.mapReduceRight({<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>},concat) <span class="comment">-- =&gt; "{'c', 'cb', 'cba'}"</span>
</pre>


<h3>include (t, value)</h3>
<p>*Aliases: <code>any</code>, <code>some</code>, <code>contains</code>*.</p>

<p>Looks for a value in a table.</p>


<pre>
M.include({<span class="number">6</span>,<span class="number">8</span>,<span class="number">10</span>,<span class="number">16</span>,<span class="number">29</span>},<span class="number">16</span>) <span class="comment">-- =&gt; true
</span>M.include({<span class="number">6</span>,<span class="number">8</span>,<span class="number">10</span>,<span class="number">16</span>,<span class="number">29</span>},<span class="number">1</span>) <span class="comment">-- =&gt; false
</span>
<span class="keyword">local</span> complex_table = {<span class="number">18</span>,{<span class="number">2</span>,{<span class="number">3</span>}}}
<span class="keyword">local</span> collection = {<span class="number">6</span>,{<span class="number">18</span>,{<span class="number">2</span>,<span class="number">6</span>}},<span class="number">10</span>,{<span class="number">18</span>,{<span class="number">2</span>,{<span class="number">3</span>}}},<span class="number">29</span>}
M.include(collection, complex_table) <span class="comment">-- =&gt; true</span>
</pre>


<p>Handles iterator functions.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> isUpper(v) <span class="keyword">return</span> v:upper()== v <span class="keyword">end</span>
M.include({<span class="string">'a'</span>,<span class="string">'B'</span>,<span class="string">'c'</span>},isUpper) <span class="comment">-- =&gt; true</span>
</pre>


<h3>detect (t, value)</h3>

<p>Returns the index of a value in a table.</p>


<pre>
M.detect({<span class="number">6</span>,<span class="number">8</span>,<span class="number">10</span>,<span class="number">16</span>},<span class="number">8</span>) <span class="comment">-- =&gt; 2
</span>M.detect({<span class="keyword">nil</span>,<span class="keyword">true</span>,<span class="number">0</span>,<span class="keyword">true</span>,<span class="keyword">true</span>},<span class="keyword">false</span>) <span class="comment">-- =&gt; nil
</span>
<span class="keyword">local</span> complex_table = {<span class="number">18</span>,{<span class="number">2</span>,<span class="number">6</span>}}
<span class="keyword">local</span> collection = {<span class="number">6</span>,{<span class="number">18</span>,{<span class="number">2</span>,<span class="number">6</span>}},<span class="number">10</span>,{<span class="number">18</span>,{<span class="number">2</span>,{<span class="number">3</span>}}},<span class="number">29</span>}
M.detect(collection, complex_table) <span class="comment">-- =&gt; 2</span>
</pre>


<p>Handles iterator functions.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> isUpper(v)
  <span class="keyword">return</span> v:upper()==v
<span class="keyword">end</span>
M.detect({<span class="string">'a'</span>,<span class="string">'B'</span>,<span class="string">'c'</span>},isUpper) <span class="comment">-- =&gt; 2</span>
</pre>


<h3>where (t, props)</h3>

<p>Looks through a table and returns all the values that matches all of the key-value pairs listed in <code>props</code>. </p>


<pre>
<span class="keyword">local</span> items = {
  {height = <span class="number">10</span>, weight = <span class="number">8</span>, price = <span class="number">500</span>},
  {height = <span class="number">10</span>, weight = <span class="number">15</span>, price = <span class="number">700</span>},
  {height = <span class="number">15</span>, weight = <span class="number">15</span>, price = <span class="number">3000</span>},
  {height = <span class="number">10</span>, weight = <span class="number">8</span>, price = <span class="number">3000</span>},
}
M.where(items, {height = <span class="number">10</span>}) <span class="comment">-- =&gt; {items[1], items[2], items[4]}
</span>M.where(items, {weight = <span class="number">15</span>}) <span class="comment">-- =&gt; {items[2], items[3]}
</span>M.where(items, {prince = <span class="number">3000</span>}) <span class="comment">-- =&gt; {items[3], items[4]}
</span>M.where(items, {height = <span class="number">10</span>, weight = <span class="number">15</span>, prince = <span class="number">700</span>}) <span class="comment">-- =&gt; {items[2]}</span>
</pre>


<h3>findWhere (t, props)</h3>

<p>Looks through a table and returns the first value found that matches all of the key-value pairs listed in <code>props</code>. </p>


<pre>
<span class="keyword">local</span> a = {a = <span class="number">1</span>, b = <span class="number">2</span>, c = <span class="number">3</span>}
<span class="keyword">local</span> b = {a = <span class="number">2</span>, b = <span class="number">3</span>, d = <span class="number">4</span>}
<span class="keyword">local</span> c = {a = <span class="number">3</span>, b = <span class="number">4</span>, e = <span class="number">5</span>}
M.findWhere({a, b, c}, {a = <span class="number">3</span>, b = <span class="number">4</span>}) == c <span class="comment">-- =&gt; true</span>
</pre>


<h3>select (t, f)</h3>
<p>*Aliases: <code>filter</code>*.</p>

<p>Collects values passing a validation test.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> isEven(v) <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> isOdd(v) <span class="keyword">return</span> v%<span class="number">2</span>~=<span class="number">0</span> <span class="keyword">end</span>

M.<span class="global">select</span>({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>}, isEven) <span class="comment">-- =&gt; "{2,4,6}"
</span>M.<span class="global">select</span>({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>}, isOdd) <span class="comment">-- =&gt; "{1,3,5,7}"</span>
</pre>


<h3>reject (t, f)</h3>
<p>*Aliases: <a href="index.html#reject">reject</a>*.</p>

<p>Removes all values failing (returning false or nil) a validation test:</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> isEven(v) <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> isOdd(v) <span class="keyword">return</span> v%<span class="number">2</span>~=<span class="number">0</span> <span class="keyword">end</span>

M.reject({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>}, isEven) <span class="comment">-- =&gt; "{1,3,5,7}"
</span>M.reject({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>}, isOdd) <span class="comment">-- =&gt; "{2,4,6}"</span>
</pre>


<h3>all (t, f)</h3>
<p>*Aliases: <code>every</code>*.</p>

<p>Checks whether or not all elements pass a validation test.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> isEven(v) <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span> <span class="keyword">end</span>
M.all({<span class="number">2</span>,<span class="number">4</span>,<span class="number">6</span>}, isEven) <span class="comment">-- =&gt; true</span>
</pre>


<h3>invoke (t, method)</h3>

<p>Invokes a given function on each value in a table.</p>


<pre>
M.invoke({<span class="string">'a'</span>,<span class="string">'bea'</span>,<span class="string">'cdhza'</span>},<span class="global">string</span>.len) <span class="comment">-- =&gt; "{1,3,5}"</span>
</pre>


<p>Can reference the method of the same name in each value.</p>


<pre>
<span class="keyword">local</span> a, b, c, d = {id = <span class="string">'a'</span>}, {id = <span class="string">'b'</span>}, {id = <span class="string">'c'</span>}, {id = <span class="string">'d'</span>}
<span class="keyword">local</span> <span class="keyword">function</span> call(self) <span class="keyword">return</span> self.id <span class="keyword">end</span>
M.invoke({a,b,c,d},call) <span class="comment">-- =&gt; "{'a','b','c','d'}"</span>
</pre>


<h3>pluck (t, property)</h3>

<p>Fetches all values indexed with specific key in a table of objects.</p>


<pre>
<span class="keyword">local</span> peoples = {
  {name = <span class="string">'John'</span>, age = <span class="number">23</span>},{name = <span class="string">'Peter'</span>, age = <span class="number">17</span>},
  {name = <span class="string">'Steve'</span>, age = <span class="number">15</span>},{age = <span class="number">33</span>}}

M.pluck(peoples,<span class="string">'age'</span>) <span class="comment">-- =&gt; "{23,17,15,33}"
</span>M.pluck(peoples,<span class="string">'name'</span>) <span class="comment">-- =&gt; "{'John', 'Peter', 'Steve'}"</span>
</pre>


<h3>max (t [, transform])</h3>

<p>Returns the maximum value in a collection.</p>


<pre>
M.max {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>} <span class="comment">-- =&gt; 3
</span>M.max {<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>} <span class="comment">-- =&gt; 'c'</span>
</pre>


<p>Can take an iterator function to extract a specific property.</p>


<pre>
<span class="keyword">local</span> peoples = {
  {name = <span class="string">'John'</span>, age = <span class="number">23</span>},{name = <span class="string">'Peter'</span>, age = <span class="number">17</span>},
  {name = <span class="string">'Steve'</span>, age = <span class="number">15</span>},{age = <span class="number">33</span>}}
M.max(peoples,<span class="keyword">function</span>(people) <span class="keyword">return</span> people.age <span class="keyword">end</span>) <span class="comment">-- =&gt; 33</span>
</pre>


<h3>min (t [, transform])</h3>

<p>Returns the minimum value in a collection.</p>


<pre>
M.min {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>} <span class="comment">-- =&gt; 1
</span>M.min {<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>} <span class="comment">-- =&gt; 'a'</span>
</pre>


<p>Can take an iterator function to extract a specific property.</p>


<pre>
<span class="keyword">local</span> peoples = {
  {name = <span class="string">'John'</span>, age = <span class="number">23</span>},{name = <span class="string">'Peter'</span>, age = <span class="number">17</span>},
  {name = <span class="string">'Steve'</span>, age = <span class="number">15</span>},{age = <span class="number">33</span>}}
M.min(peoples,<span class="keyword">function</span>(people) <span class="keyword">return</span> people.age <span class="keyword">end</span>) <span class="comment">-- =&gt; 15</span>
</pre>


<h3>same (a, b)</h3>

<p>Tests whether or not all values in each of the passed-in tables exists in both tables.</p>


<pre>
<span class="keyword">local</span> a = {<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>}
<span class="keyword">local</span> b = {<span class="string">'b'</span>,<span class="string">'a'</span>,<span class="string">'d'</span>,<span class="string">'c'</span>}
M.same(a,b) <span class="comment">-- =&gt; true
</span>
b[#b+<span class="number">1</span>] = <span class="string">'e'</span>
M.same(a,b) <span class="comment">-- =&gt; false</span>
</pre>


<h3>sort (t [, comp = math.min])</h3>

<p>Sorts a collection.</p>


<pre>
M.sort({<span class="string">'b'</span>,<span class="string">'a'</span>,<span class="string">'d'</span>,<span class="string">'c'</span>}) <span class="comment">-- =&gt; "{'a','b','c','d'}"</span>
</pre>


<p>Handles custom comparison functions.</p>


<pre>
M.sort({<span class="string">'b'</span>,<span class="string">'a'</span>,<span class="string">'d'</span>,<span class="string">'c'</span>}, <span class="keyword">function</span>(a,b)
  <span class="keyword">return</span> a:byte() &gt; b:byte()
<span class="keyword">end</span>) <span class="comment">-- =&gt; "{'d','c','b','a'}"</span>
</pre>


<h3>sortedk (t [, comp])</h3>

<p>Iterates on values with respect to key order. Keys are sorted using <code>comp</code> function which defaults to <a href="https://www.lua.org/manual/5.1/manual.html#pdf-math.min">math.min</a>.
It returns upon each call a <code>key, value</code> pair.</p>


<pre>
<span class="keyword">local</span> tbl = {}; tbl[<span class="number">3</span>] = <span class="number">5</span> ; tbl[<span class="number">2</span>] = <span class="number">6</span>; tbl[<span class="number">5</span>] = <span class="number">8</span>; tbl[<span class="number">4</span>] = <span class="number">10</span>; tbl[<span class="number">1</span>] = <span class="number">12</span>
<span class="keyword">for</span> k, v <span class="keyword">in</span> M.sortedk(tbl) <span class="keyword">do</span> <span class="global">print</span>(k, v) <span class="keyword">end</span>

<span class="comment">-- =&gt; 1    12
</span><span class="comment">-- =&gt; 2 6
</span><span class="comment">-- =&gt; 3 5
</span><span class="comment">-- =&gt; 4 10
</span><span class="comment">-- =&gt; 5 8
</span>
<span class="keyword">local</span> <span class="keyword">function</span> comp(a,b) <span class="keyword">return</span> a &gt; b <span class="keyword">end</span>
<span class="keyword">for</span> k, v <span class="keyword">in</span> M.sortedk(tbl, comp) <span class="keyword">do</span> <span class="global">print</span>(k, v) <span class="keyword">end</span>

<span class="comment">-- =&gt; 5    8
</span><span class="comment">-- =&gt; 4 10
</span><span class="comment">-- =&gt; 3 5
</span><span class="comment">-- =&gt; 2 6
</span><span class="comment">-- =&gt; 1 12</span>
</pre>


<h3>sortedv (t [, comp])</h3>

<p>Iterates on values with respect to key order. Keys are sorted using <code>comp</code> function which defaults to <a href="https://www.lua.org/manual/5.1/manual.html#pdf-math.min">math.min</a>.
It returns upon each call a <code>key, value</code> pair.</p>


<pre>
<span class="keyword">local</span> tbl = {}; tbl[<span class="number">3</span>] = <span class="number">5</span> ; tbl[<span class="number">2</span>] = <span class="number">6</span>; tbl[<span class="number">5</span>] = <span class="number">8</span>; tbl[<span class="number">4</span>] = <span class="number">10</span>; tbl[<span class="number">1</span>] = <span class="number">12</span>
<span class="keyword">for</span> k, v <span class="keyword">in</span> M.sortedv(tbl) <span class="keyword">do</span> <span class="global">print</span>(k, v) <span class="keyword">end</span>

<span class="comment">-- =&gt; 3    5
</span><span class="comment">-- =&gt; 2 6
</span><span class="comment">-- =&gt; 5 8
</span><span class="comment">-- =&gt; 4 10
</span><span class="comment">-- =&gt; 1 12
</span>
<span class="keyword">local</span> <span class="keyword">function</span> comp(a,b) <span class="keyword">return</span> a &gt; b <span class="keyword">end</span>
<span class="keyword">for</span> k, v <span class="keyword">in</span> M.sortedv(tbl, comp) <span class="keyword">do</span> <span class="global">print</span>(k, v) <span class="keyword">end</span>

<span class="comment">-- =&gt; 1    12
</span><span class="comment">-- =&gt; 4 10
</span><span class="comment">-- =&gt; 5 8
</span><span class="comment">-- =&gt; 2 6
</span><span class="comment">-- =&gt; 3 5</span>
</pre>


<h3>sortBy (t [, transform [, comp = math.min]])</h3>

<p>Sorts items in a collection based on the result of running a transform function through every item in the collection.</p>


<pre>
<span class="keyword">local</span> r = M.sortBy({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>}, <span class="global">math</span>.sin)
<span class="global">print</span>(<span class="global">table</span>.concat(r,<span class="string">','</span>))

<span class="comment">-- =&gt; {5,4,3,1,2}</span>
</pre>


<p>The transform function can also be a string name property.</p>


<pre>
<span class="keyword">local</span> people = {
    {name = <span class="string">'albert'</span>, age = <span class="number">40</span>},
    {name = <span class="string">'louis'</span>, age = <span class="number">55</span>},
    {name = <span class="string">'steve'</span>, age = <span class="number">35</span>},
    {name = <span class="string">'henry'</span>, age = <span class="number">19</span>},
}
<span class="keyword">local</span> r = M.sortBy(people, <span class="string">'age'</span>)
M.each(r, <span class="keyword">function</span>(v) <span class="global">print</span>(v.age, v.name) <span class="keyword">end</span>)

<span class="comment">-- =&gt; 19   henry
</span><span class="comment">-- =&gt; 35    steve
</span><span class="comment">-- =&gt; 40    albert
</span><span class="comment">-- =&gt; 55    louis</span>
</pre>


<p>As seen above, the defaut comparison function is the '&lt;' operator. For example, let us supply a different one to sort the list of people by decreasing age order :</p>


<pre>
<span class="keyword">local</span> people = {
    {name = <span class="string">'albert'</span>, age = <span class="number">40</span>},
    {name = <span class="string">'louis'</span>, age = <span class="number">55</span>},
    {name = <span class="string">'steve'</span>, age = <span class="number">35</span>},
    {name = <span class="string">'henry'</span>, age = <span class="number">19</span>},
}
<span class="keyword">local</span> r = M.sortBy(people, <span class="string">'age'</span>, <span class="keyword">function</span>(a,b) <span class="keyword">return</span> a &gt; b <span class="keyword">end</span>)
M.each(r, <span class="keyword">function</span>(v) <span class="global">print</span>(v.age, v.name) <span class="keyword">end</span>)

<span class="comment">-- =&gt; 55   louis
</span><span class="comment">-- =&gt; 40    albert
</span><span class="comment">-- =&gt; 35    steve
</span><span class="comment">-- =&gt; 19    henry</span>
</pre>


<p>The <code>transform</code> function defaults to <code>M.indentity</code> and in that case, <code>M.sortBy</code> behaves like <code>M.sort</code>.</p>


<pre>
<span class="keyword">local</span> r = M.sortBy({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>})
<span class="global">print</span>(<span class="global">table</span>.concat(r,<span class="string">','</span>))

<span class="comment">-- =&gt; {1,2,3,4,5}</span>
</pre>


<h3>groupBy (t, iter)</h3>

<p>Groups values in a collection depending on their return value when passed to a predicate test.</p>


<pre>
M.groupBy({<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>},<span class="keyword">function</span>(v)
  <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span> <span class="keyword">and</span> <span class="string">'even'</span> <span class="keyword">or</span> <span class="string">'odd'</span>
<span class="keyword">end</span>)
<span class="comment">-- =&gt; "{odd = {1,3,5}, even = {0,2,4,6}}"
</span>
M.groupBy({<span class="number">0</span>,<span class="string">'a'</span>,<span class="keyword">true</span>, <span class="keyword">false</span>,<span class="keyword">nil</span>,b,<span class="number">0.5</span>},<span class="global">type</span>)
<span class="comment">-- =&gt; "{number = {0,0.5}, string = {'a'}, boolean = {true, false}}"        </span>
</pre>


<h3>countBy (t, iter)</h3>

<p>Splits a table in subsets and provide the count for each subset.</p>


<pre>
M.countBy({<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>},<span class="keyword">function</span>(v)
  <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span> <span class="keyword">and</span> <span class="string">'even'</span> <span class="keyword">or</span> <span class="string">'odd'</span>
<span class="keyword">end</span>) <span class="comment">-- =&gt; "{odd = 3, even = 4}"</span>
</pre>


<h3>size (...)</h3>

<p>When given a table, provides the count for the very number of values in that table.</p>


<pre>
M.size {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>} <span class="comment">-- =&gt; 3
</span>M.size {one = <span class="number">1</span>, two = <span class="number">2</span>} <span class="comment">-- =&gt; 2</span>
</pre>


<p>When given a vararg list of arguments, returns the count of these arguments.</p>


<pre>
M.size(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>) <span class="comment">-- =&gt; 3
</span>M.size(<span class="string">'a'</span>,<span class="string">'b'</span>,{}, <span class="keyword">function</span>() <span class="keyword">end</span>) <span class="comment">-- =&gt; 4</span>
</pre>


<h3>containsKeys (t, other)</h3>

<p>Checks whether a table has all the keys existing in another table.</p>


<pre>
M.contains({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>},{<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}) <span class="comment">-- =&gt; true
</span>M.contains({<span class="number">1</span>,<span class="number">2</span>,<span class="string">'d'</span>,<span class="string">'b'</span>},{<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">5</span>}) <span class="comment">-- =&gt; true
</span>M.contains({x = <span class="number">1</span>, y = <span class="number">2</span>, z = <span class="number">3</span>},{x = <span class="number">1</span>, y = <span class="number">2</span>}) <span class="comment">-- =&gt; true</span>
</pre>


<h3>sameKeys (tA, tB)</h3>

<p>Checks whether both tables features the same keys:</p>


<pre>
M.sameKeys({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>},{<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}) <span class="comment">-- =&gt; false
</span>M.sameKeys({<span class="number">1</span>,<span class="number">2</span>,<span class="string">'d'</span>,<span class="string">'b'</span>},{<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">5</span>}) <span class="comment">-- =&gt; true
</span>M.sameKeys({x = <span class="number">1</span>, y = <span class="number">2</span>, z = <span class="number">3</span>},{x = <span class="number">1</span>, y = <span class="number">2</span>}) <span class="comment">-- =&gt; false</span>
</pre>


<p><strong><a href="#TOC">[⬆]</a></strong></p>

<p><a name="_a_name__array__Array_functions__a_"></a></p>
<h2><a name='array'>Array functions</a></h2>

<h3>sample (array [, n = 1 [, seed]])</h3>

<p>Samples <code>n</code> values from array.</p>


<pre>
<span class="keyword">local</span> array = M.range(<span class="number">1</span>,<span class="number">20</span>)
<span class="keyword">local</span> sample = M.sample(array, <span class="number">3</span>)
<span class="global">print</span>(<span class="global">table</span>.concat(sample,<span class="string">','</span>))

<span class="comment">-- =&gt; {12,11,15}</span>
</pre>


<p><code>n</code> defaults to 1. In that case, a single value will be returned.</p>


<pre>
<span class="keyword">local</span> array = M.range(<span class="number">1</span>,<span class="number">20</span>)
<span class="keyword">local</span> sample = M.sample(array)
<span class="global">print</span>(sample)

<span class="comment">-- =&gt; 12</span>
</pre>


<p>An optional 3rd argument <code>seed</code> can be passed for deterministic random sampling.</p>

<h3>sampleProb (array, prob [, seed])</h3>

<p>Returns an array of values randomly selected from a given array.
In case <code>seed</code> is provided, it is used for deterministic sampling.</p>


<pre>
<span class="keyword">local</span> array = M.range(<span class="number">1</span>,<span class="number">20</span>)
<span class="keyword">local</span> sample = M.sampleProb(array, <span class="number">0.2</span>)
<span class="global">print</span>(<span class="global">table</span>.concat(sample,<span class="string">','</span>))

<span class="comment">-- =&gt; 5,11,12,15
</span>
sample = M.sampleProb(array, <span class="number">0.2</span>, <span class="global">os</span>.time())
<span class="global">print</span>(<span class="global">table</span>.concat(sample,<span class="string">','</span>))

<span class="comment">-- =&gt; 1,6,10,12,15,20 (or similar)</span>
</pre>


<h3>nsorted (array [, n = 1[, comp]])</h3>

<p>Returns the n-top values satisfying a predicate. It takes a comparison function <code>comp</code> used to sort array values,
and then picks the top n-values. It leaves the original array untouched.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> comp(a,b) <span class="keyword">return</span> a &gt; b <span class="keyword">end</span>
M.nsorted(array,<span class="number">5</span>, comp) <span class="comment">-- =&gt; {5,4,3,2,1}</span>
</pre>


<p><code>n</code> defaults to 1 and <code>comp</code> defaults to the <code>&lt;</code> operator.</p>


<pre>
<span class="keyword">local</span> array = M.range(<span class="number">1</span>,<span class="number">20</span>)
M.nsorted(array) <span class="comment">-- =&gt; {1}</span>
</pre>


<h3>shuffle (array [, seed])</h3>

<p>Shuffles a given array.</p>


<pre>
<span class="keyword">local</span> list = M.shuffle {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>} <span class="comment">-- =&gt; "{3,2,6,4,1,5}"
</span>M.each(list,<span class="global">print</span>)
</pre>


<h3>pack (...)</h3>

<p>Converts a vararg list of arguments to an array.</p>


<pre>
M.pack(<span class="number">1</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="string">'d'</span>,<span class="string">'a'</span>,<span class="number">0</span>) <span class="comment">-- =&gt; "{1,2,8,'d','a',0}"</span>
</pre>


<h3>find (array, value [, from = 1])</h3>

<p>Looks for a value in a given array and returns the position of the first occurence.</p>


<pre>
<span class="keyword">local</span> value = {<span class="number">3</span>}
M.find({{<span class="number">4</span>},{<span class="number">3</span>},{<span class="number">2</span>},{<span class="number">1</span>}},value) <span class="comment">-- =&gt; 2</span>
</pre>


<p>It can also start the search at a specific position in the array:</p>


<pre>
<span class="comment">-- search value 4 starting from index 3
</span>M.find({<span class="number">1</span>,<span class="number">4</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>},<span class="number">4</span>,<span class="number">3</span>) <span class="comment">-- =&gt; 5</span>
</pre>


<h3>reverse (array)</h3>

<p>Reverses an array.</p>


<pre>
M.reverse({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="string">'d'</span>}) <span class="comment">-- =&gt; "{'d',3,2,1}"</span>
</pre>


<h3>fill (array, value [, i = 1 [, j = #array]])</h3>

<p>Replaces all elements in a given array with a given value.</p>


<pre>
<span class="keyword">local</span> array = M.range(<span class="number">1</span>,<span class="number">5</span>)
M.fill(array, <span class="number">0</span>) <span class="comment">-- =&gt; {0,0,0,0,0}</span>
</pre>


<p>It can start replacing value at a specific index.</p>


<pre>
<span class="keyword">local</span> array = M.range(<span class="number">1</span>,<span class="number">5</span>)
M.fill(array,<span class="number">0</span>,<span class="number">3</span>) <span class="comment">-- =&gt; {1,2,0,0,0}</span>
</pre>


<p>It can replace only values within a specific range.</p>


<pre>
<span class="keyword">local</span> array = M.range(<span class="number">1</span>,<span class="number">5</span>)
M.fill(array,<span class="number">0</span>,<span class="number">2</span>,<span class="number">4</span>) <span class="comment">-- =&gt; {1,0,0,0,5}</span>
</pre>


<p>In case the upper bound index i greather than the array size, it will enlarge the array.</p>


<pre>
<span class="keyword">local</span> array = M.range(<span class="number">1</span>,<span class="number">5</span>)
M.fill(array,<span class="number">0</span>,<span class="number">5</span>,<span class="number">10</span>) <span class="comment">-- =&gt; {1,2,3,4,0,0,0,0,0,0}</span>
</pre>


<h3>zeros (n)</h3>

<p>Returns an array of <code>n</code> zeros.</p>


<pre>
M.zeros(<span class="number">4</span>) <span class="comment">-- =&gt; {0,0,0,0}</span>
</pre>


<h3>ones (n)</h3>

<p>Returns an array of <code>n</code> 1's.</p>


<pre>
M.ones(<span class="number">3</span>) <span class="comment">-- =&gt; {1,1,1}</span>
</pre>


<h3>vector (value, n)</h3>

<p>Returns an array of <code>n</code> times a given value.</p>


<pre>
M.vector(<span class="number">10</span>, <span class="number">4</span>) <span class="comment">-- =&gt; {10,10,10,10}</span>
</pre>


<h3>selectWhile (array, f [, ...])</h3>
<p>*Aliases: <code>takeWhile</code>*.</p>

<p>Collects values as long as they pass a given test. Stops on the first non-passing test.</p>


<pre>
M.selectWhile({<span class="number">2</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">8</span>}, <span class="keyword">function</span>(v)
  <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span>
<span class="keyword">end</span>) <span class="comment">-- =&gt; "{2,4}"</span>
</pre>


<h3>dropWhile (array, f [, ...])</h3>
<p>*Aliases: <code>rejectWhile</code>*.</p>

<p>Removes values as long as they pass a given test. Stops on the first non-passing test.</p>


<pre>
M.dropWhile({<span class="number">2</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">8</span>}, <span class="keyword">function</span>(v)
  <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span>
<span class="keyword">end</span>) <span class="comment">-- =&gt; "{5,8}"</span>
</pre>


<h3>sortedIndex (array, value [, comp = math.min [, sort = nil]])</h3>

<p>Returns the index at which a value should be inserted to preserve order.</p>


<pre>
M.sortedIndex({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>},<span class="number">4</span>) <span class="comment">-- =&gt; 4</span>
</pre>


<p>Can take a custom comparison functions.</p>


<pre>
<span class="keyword">local</span> comp = <span class="keyword">function</span>(a,b) <span class="keyword">return</span> a&lt;b <span class="keyword">end</span>
M.sortedIndex({-<span class="number">5</span>,<span class="number">0</span>,<span class="number">4</span>,<span class="number">4</span>},<span class="number">3</span>,comp) <span class="comment">-- =&gt; 3</span>
</pre>


<h3>indexOf (array, value)</h3>

<p>Returns the index of a value in an array.</p>


<pre>
M.indexOf({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>},<span class="number">2</span>) <span class="comment">-- =&gt; 2</span>
</pre>


<h3>lastIndexOf (array, value)</h3>

<p>Returns the index of the last occurence of a given value in an array.</p>


<pre>
M.lastIndexOf({<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">3</span>},<span class="number">2</span>) <span class="comment">-- =&gt; 3</span>
</pre>


<h3>findIndex (array, pred)</h3>

<p>Returns the first index at which a predicate passes a truth test.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>}
<span class="keyword">local</span> <span class="keyword">function</span> multipleOf3(v) <span class="keyword">return</span> v%<span class="number">3</span>==<span class="number">0</span> <span class="keyword">end</span>
M.findIndex(array, multipleOf3) <span class="comment">-- =&gt; 3</span>
</pre>


<h3>findLastIndex (array, pred)</h3>

<p>Returns the last index at which a predicate passes a truthy test.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>}
<span class="keyword">local</span> <span class="keyword">function</span> multipleOf3(v) <span class="keyword">return</span> v%<span class="number">3</span>==<span class="number">0</span> <span class="keyword">end</span>
M.findLastIndex(array, multipleOf3) <span class="comment">-- =&gt; 6</span>
</pre>


<h3>addTop (array, ...)</h3>

<p>Adds given values at the top of an array. The latter values bubbles at the top.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>}
M.addTop(array,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>) <span class="comment">-- =&gt; "{4,3,2,1,1}"</span>
</pre>


<h3>prepend (array, ...)</h3>

<p>Adds given values at the top of an array, preserving the order at which elements are passed-in.</p>


<pre>
<span class="keyword">local</span> array = {<span class="string">'old_val'</span>}
M.prepend(array,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>) <span class="comment">-- =&gt; "{1,2,3,4,'old_val'}"</span>
</pre>


<h3>push (array, ...)</h3>

<p>Adds given values at the end of an array.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>}
M.push(array,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>) <span class="comment">-- =&gt; "{1,1,2,3,4}"</span>
</pre>


<h3>shift (array [, n = 1])</h3>
<p>*Aliases: <code>pop</code>*.</p>

<p>Removes and returns the first value in an array.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}
<span class="keyword">local</span> shift = M.shift(array) <span class="comment">-- =&gt; "shift = 1", "array = {2,3}"</span>
</pre>

<p>If <code>n</code> is supplied, returns <code>n</code> values.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>}
<span class="keyword">local</span> a, b = M.shift(array, <span class="number">2</span>) <span class="comment">-- =&gt; "a = 1, b = 2", "array = {3,4,5}"</span>
</pre>


<h3>unshift (array [, n = 1])</h3>

<p>Removes and returns the last value in an array.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}
<span class="keyword">local</span> value = M.unshift(array) <span class="comment">-- =&gt; "value = 3", "array = {1,2}"</span>
</pre>


<h3>pull (array, ...)</h3>
<p>*Aliases: <code>remove</code>*.</p>

<p>Removes all provided values from a given array.</p>


<pre>
M.pull({<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">3</span>},<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>) <span class="comment">-- =&gt; "{4}"</span>
</pre>


<h3>removeRange (array [, start = 1 [, finish = #array]])</h3>
<p>*Aliases: <code>rmRange</code>, <code>M.chop</code>*.</p>

<p>Trims out all values index within a range.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>}
M.removeRange(array, <span class="number">3</span>,<span class="number">8</span>) <span class="comment">-- =&gt; "{1,2,9}"</span>
</pre>


<h3>chunk (array [, f])</h3>

<p>Iterates over an array aggregating consecutive values in subsets tables, on the basis of the return value of <code>f(v, k, ...)</code>. Consecutive elements which return the same value are chunked together.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">5</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">4</span>}
M.chunk(t, <span class="keyword">function</span>(v) <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span> <span class="keyword">end</span>) <span class="comment">-- =&gt; "{{1,5},{2,4},{3,3},{4}}"</span>
</pre>


<p>If not given, <code>f</code> defaults to <a href="index.html#identity">identity</a>.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">5</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">4</span>}
M.chunk(t) <span class="comment">-- =&gt; "{{1},{5},{2},{4},{3,3},{4}}"</span>
</pre>


<h3>slice (array [, start = 1 [, finish = #array]])</h3>
<p>*Aliases: <a href="index.html#operator.sub">sub</a>*.</p>

<p>Slices and returns a part of an array.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>}
M.slice(array, <span class="number">3</span>,<span class="number">6</span>) <span class="comment">-- =&gt; "{3,4,5,6}"</span>
</pre>


<h3>first (array [, n = 1])</h3>
<p>*Aliases: <code>head</code>, <code>M.take</code>*.</p>

<p>Returns the first N elements in an array.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>}
M.first(array,<span class="number">3</span>) <span class="comment">-- =&gt; "{1,2,3}"</span>
</pre>


<h3>initial (array [, n = #array])</h3>

<p>Excludes the last N elements in an array.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>}
M.initial(array,<span class="number">5</span>) <span class="comment">-- =&gt; "{1,2,3,4}"</span>
</pre>


<h3>last (array [, n = #array])</h3>

<p>Returns the last N elements in an array.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>}
M.last(array,<span class="number">3</span>) <span class="comment">-- =&gt; "{7,8,9}"</span>
</pre>


<h3>rest (array [, index = 1])</h3>
<p>*Aliases: <code>tail</code>*.</p>

<p>Returns all values after <em>index</em>, including the given <em>index</em> itself.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>}
M.rest(array,<span class="number">6</span>) <span class="comment">-- =&gt; "{6,7,8,9}"</span>
</pre>


<h3>nth (array, index)</h3>

<p>Returns the value at <em>index</em>.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>}
M.nth(array,<span class="number">3</span>) <span class="comment">-- =&gt; "3"</span>
</pre>


<h3>compact (array)</h3>

<p>Trims out all falsy values.</p>


<pre>
M.compact {a,<span class="string">'aa'</span>,<span class="keyword">false</span>,<span class="string">'bb'</span>,<span class="keyword">true</span>} <span class="comment">-- =&gt; "{'aa','bb',true}"</span>
</pre>


<h3>flatten (array [, shallow = false])</h3>

<p>Flattens a nested array.</p>


<pre>
M.flatten({<span class="number">1</span>,{<span class="number">2</span>,<span class="number">3</span>},{<span class="number">4</span>,<span class="number">5</span>,{<span class="number">6</span>,<span class="number">7</span>}}}) <span class="comment">-- =&gt; "{1,2,3,4,5,6,7}"</span>
</pre>


<p>When given arg <code>shallow</code>, flatten only at the first level.</p>


<pre>
M.flatten({<span class="number">1</span>,{<span class="number">2</span>},{{<span class="number">3</span>}}},<span class="keyword">true</span>) <span class="comment">-- =&gt; "{1,{2},{{3}}}"</span>
</pre>


<h3>difference (array, array2)</h3>
<p>*Aliases: <code>without</code>, <code>diff</code>*.</p>

<p>Returns values in the given array not present in a second array.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="string">'a'</span>,<span class="number">4</span>,<span class="number">5</span>}
M.difference(array,{<span class="number">1</span>,<span class="string">'a'</span>}) <span class="comment">-- =&gt; "{2,4,5}"</span>
</pre>


<h3>union (...)</h3>

<p>Produces a duplicate-free union of all passed-in arrays.</p>


<pre>
<span class="keyword">local</span> A = {<span class="string">'a'</span>}
<span class="keyword">local</span> B = {<span class="string">'a'</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}
<span class="keyword">local</span> C = {<span class="number">2</span>,<span class="number">10</span>}
M.union(A,B,C) <span class="comment">-- =&gt; "{'a',1,2,3,10}"</span>
</pre>


<h3>intersection (...)</h3>

<p>Returns the intersection (common-part) of all passed-in arrays:</p>


<pre>
<span class="keyword">local</span> A = {<span class="string">'a'</span>}
<span class="keyword">local</span> B = {<span class="string">'a'</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}
<span class="keyword">local</span> C = {<span class="number">2</span>,<span class="number">10</span>,<span class="number">1</span>,<span class="string">'a'</span>}
M.intersection(A,B,C) <span class="comment">-- =&gt; "{'a'}"</span>
</pre>


<h3>disjoint (...)</h3>

<p>Checks if all passed in arrays are disjoint.</p>


<pre>
<span class="keyword">local</span> A = {<span class="string">'a'</span>}
<span class="keyword">local</span> B = {<span class="string">'a'</span>,<span class="number">1</span>,<span class="number">3</span>}
<span class="keyword">local</span> C = {<span class="number">3</span>,<span class="number">10</span>,<span class="number">2</span>}

M.disjoint(A,B) <span class="comment">-- =&gt; false
</span>M.disjoint(A,C) <span class="comment">-- =&gt; true
</span>M.disjoint(B,C) <span class="comment">-- =&gt; false</span>
</pre>


<h3>symmetricDifference (array, array2)</h3>
<p>*Aliases: <code>symdiff</code>,<code>xor</code>*.</p>

<p>Returns values in the first array not present in the second and also values in the second array not present in the first one.</p>


<pre>
<span class="keyword">local</span> array = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}
<span class="keyword">local</span> array2 = {<span class="number">1</span>,<span class="number">4</span>,<span class="number">5</span>}
M.symmetricDifference(array, array2) <span class="comment">-- =&gt; "{2,3,4,5}"</span>
</pre>


<h3>unique (array)</h3>
<p>*Aliases: <code>uniq</code>*.</p>

<p>Makes an array duplicate-free.</p>


<pre>
M.unique {<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">5</span>} <span class="comment">-- =&gt; "{1,2,3,4,5}"</span>
</pre>


<h3>isunique (array)</h3>
<p>*Aliases: <code>isuniq</code>*.</p>

<p>Checks if a given array contains no duplicate value.</p>


<pre>
M.isunique({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>}) <span class="comment">-- =&gt; true
</span>M.isunique({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">4</span>}) <span class="comment">-- =&gt; false</span>
</pre>


<h3>duplicates (array)</h3>

<p>Returns an array list of all duplicates in array.</p>


<pre>
M.duplicates({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">8</span>,<span class="number">8</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">4</span>}) <span class="comment">-- =&gt; {2,3,8}</span>
</pre>


<h3>zip (...)</h3>
<p>*Aliases: <code>transpose</code>*.</p>

<p>Zips values from different arrays, on the basis on their common keys.</p>


<pre>
<span class="keyword">local</span> names = {<span class="string">'Bob'</span>,<span class="string">'Alice'</span>,<span class="string">'James'</span>}
<span class="keyword">local</span> ages = {<span class="number">22</span>, <span class="number">23</span>}
M.zip(names,ages) <span class="comment">-- =&gt; "{{'Bob',22},{'Alice',23},{'James'}}"</span>
</pre>


<h3>zipWith (f, ...)</h3>
<p>*Aliases: <code>transposeWith</code>*.</p>

<p>Merges values using a given function. Only values indexed with the same key in the given arrays are merged in the same subset.
Function <code>f</code> is used to combine values.</p>


<pre>
<span class="keyword">local</span> names = {<span class="string">'Bob'</span>,<span class="string">'Alice'</span>,<span class="string">'James'</span>}; <span class="keyword">local</span> ages = {<span class="number">22</span>, <span class="number">23</span>, <span class="number">25</span>}
<span class="keyword">local</span> <span class="keyword">function</span> introduce(name, age) <span class="keyword">return</span> <span class="string">'I am '</span>..name..<span class="string">' and I am '</span>..age..<span class="string">' years old.'</span> <span class="keyword">end</span>
<span class="keyword">local</span> t = M.zipWith(introduce,names,ages)
<span class="comment">-- =&gt; {
</span><span class="comment">-- =&gt;  'I am Bob and I am 22 years old.'
</span><span class="comment">-- =&gt;  'I am Alice and I am 23 years old.'
</span><span class="comment">-- =&gt;  'I am James and I am 25 years old.'
</span><span class="comment">-- =&gt; }</span>
</pre>


<h3>append (array, other)</h3>

<p>Appends two arrays.</p>


<pre>
M.append({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>},{<span class="string">'a'</span>,<span class="string">'b'</span>}) <span class="comment">-- =&gt; "{1,2,3,'a','b'}"</span>
</pre>


<h3>interleave (...)</h3>

<p>Interleaves values from passed-in arrays.</p>


<pre>
t1 = {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>}
t2 = {<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>}
M.interleave(t1, t2) <span class="comment">-- =&gt; "{1,'a',2,'b',3,'c'}"</span>
</pre>


<h3>interpose (array, value)</h3>
<p>*Aliases: <code>intersperce</code>*.</p>

<p>Interposes a value between consecutive values in an arrays.</p>


<pre>
M.interleave(<span class="string">'a'</span>, {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}) <span class="comment">-- =&gt; "{1,'a',2,'a',3}"</span>
</pre>


<h3>range ([from [, to [, step]]])</h3>

<p>Generates an arithmetic sequence.</p>


<pre>
M.range(<span class="number">1</span>,<span class="number">4</span>) <span class="comment">-- =&gt; "{1,2,3,4}"</span>
</pre>


<p>In case a single value is provided, it generates a sequence from 1 to that value.</p>


<pre>
M.range(<span class="number">3</span>) <span class="comment">-- =&gt; "{1,2,3}"</span>
</pre>


<p>The incremental step can also be provided as third argument.</p>


<pre>
M.range(<span class="number">0</span>,<span class="number">2</span>,<span class="number">0.7</span>) <span class="comment">-- =&gt; "{0,0.7,1.4}"</span>
</pre>


<p>It also handles negative progressions.</p>


<pre>
M.range(-<span class="number">5</span>) <span class="comment">-- =&gt; "{-1,-2,-3,-4,-5}"
</span>M.range(<span class="number">5</span>,<span class="number">1</span>) <span class="comment">-- =&gt; "{5,4,3,2,1}"</span>
</pre>


<h3>rep (value, n)</h3>

<p>Generates a list of n repetitions of a value.</p>


<pre>
M.rep(<span class="number">4</span>,<span class="number">3</span>) <span class="comment">-- =&gt; "{4,4,4}"</span>
</pre>


<h3>powerset (array)</h3>

<p>Returns the powerset of an array.</p>


<pre>
M.powerset {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>} <span class="comment">-- =&gt; "{{1},{2},{3},{1,2},{2,3},{1,2,3}}"</span>
</pre>


<h3>partition (array [, n = 1 [, pad]])</h3>
<p>*Aliases: <code>part</code>*.</p>

<p>Returns an iterator function for partitions of a given array.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>}
<span class="keyword">for</span> p <span class="keyword">in</span> M.partition(t,<span class="number">2</span>) <span class="keyword">do</span>
  <span class="global">print</span>(<span class="global">table</span>.concat(p, <span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2
</span><span class="comment">-- =&gt; 3,4
</span><span class="comment">-- =&gt; 5,6
</span>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>}
<span class="keyword">for</span> p <span class="keyword">in</span> M.partition(t,<span class="number">4</span>) <span class="keyword">do</span>
  <span class="global">print</span>(<span class="global">table</span>.concat(p, <span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2,3,4
</span><span class="comment">-- =&gt; 5,6</span>
</pre>


<p>In case the last partition has less elements than desired, a 3rd argument can be supplied to adjust the partition size.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>}
<span class="keyword">for</span> p <span class="keyword">in</span> M.partition(t,<span class="number">4</span>,<span class="number">0</span>) <span class="keyword">do</span>
  <span class="global">print</span>(<span class="global">table</span>.concat(p, <span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2,3,4
</span><span class="comment">-- =&gt; 5,6,0,0</span>
</pre>


<h3>overlapping (array [, n = 2 [, pad]])</h3>

<p>Returns an iterator function which provides overlapping subsequences of a given array.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>}
<span class="keyword">for</span> p <span class="keyword">in</span> M.overlapping(t,<span class="number">3</span>) <span class="keyword">do</span>
    <span class="global">print</span>(<span class="global">table</span>.concat(p,<span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2,3
</span><span class="comment">-- =&gt; 3,4,5
</span><span class="comment">-- =&gt; 5,6,7
</span>
<span class="keyword">for</span> p <span class="keyword">in</span> M.overlapping(t,<span class="number">4</span>) <span class="keyword">do</span>
    <span class="global">print</span>(<span class="global">table</span>.concat(p,<span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2,3,4
</span><span class="comment">-- =&gt; 4,5,6,7
</span>
<span class="keyword">for</span> p <span class="keyword">in</span> M.overlapping(t,<span class="number">5</span>) <span class="keyword">do</span>
    <span class="global">print</span>(<span class="global">table</span>.concat(p,<span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2,3,4,5
</span><span class="comment">-- =&gt; 5,6,7</span>
</pre>


<p>In case the last subsequence wil not match the exact desired length, it can be adjusted with a 3rd argument <code>pad</code>.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>}
<span class="keyword">for</span> p <span class="keyword">in</span> M.overlapping(t,<span class="number">5</span>,<span class="number">0</span>) <span class="keyword">do</span>
    <span class="global">print</span>(<span class="global">table</span>.concat(p,<span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2,3,4,5
</span><span class="comment">-- =&gt; 5,6,7,0,0</span>
</pre>


<h3>aperture (array [, n = 2])</h3>
<p>*Aliases: <code>sliding</code>*.</p>

<p>Returns an iterator function which provides sliding partitions of a given array.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>}
<span class="keyword">for</span> p <span class="keyword">in</span> M.aperture(t,<span class="number">4</span>) <span class="keyword">do</span>
  <span class="global">print</span>(<span class="global">table</span>.concat(p,<span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2,3,4
</span><span class="comment">-- =&gt; 2,3,4,5
</span>
<span class="keyword">for</span> p <span class="keyword">in</span> M.aperture(t,<span class="number">3</span>) <span class="keyword">do</span>
  <span class="global">print</span>(<span class="global">table</span>.concat(p,<span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2,3
</span><span class="comment">-- =&gt; 2,3,4
</span><span class="comment">-- =&gt; 3,4,5</span>
</pre>


<h3>pairwise (array)</h3>

<p>Iterator returning sliding pairs of an array.</p>


<pre>
<span class="keyword">local</span> t = M.range(<span class="number">5</span>)
<span class="keyword">for</span> p <span class="keyword">in</span> pairwise(t) <span class="keyword">do</span>
  <span class="global">print</span>(<span class="global">table</span>.concat(p,<span class="string">','</span>))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 1,2
</span><span class="comment">-- =&gt; 2,3
</span><span class="comment">-- =&gt; 3,4
</span><span class="comment">-- =&gt; 4,5</span>
</pre>


<h3>permutation (array)</h3>
<p>*Aliases: <code>perm</code>*.</p>

<p>Returns an iterator function for permutations of a given array.</p>


<pre>
t = {<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>}
<span class="keyword">for</span> p <span class="keyword">in</span> M.permutation(t) <span class="keyword">do</span>
  <span class="global">print</span>(<span class="global">table</span>.concat(p))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 'bca'
</span><span class="comment">-- =&gt; 'cba'
</span><span class="comment">-- =&gt; 'cab'
</span><span class="comment">-- =&gt; 'acb'
</span><span class="comment">-- =&gt; 'bac'
</span><span class="comment">-- =&gt; 'abc'</span>
</pre>


<h3>concat (array [, sep = '' [, i = 1 [, j = #array]]])</h3>
<p>*Aliases: <code>join</code>*.</p>

<p>Concatenates a given array values:</p>


<pre>
M.concat({<span class="string">'a'</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="string">'b'</span>}) <span class="comment">-- =&gt; 'a101b'</span>
</pre>


<h3>xprod (array, array2)</h3>

<p>Returns all possible pairs built from given arrays.</p>


<pre>
<span class="keyword">local</span> t = M.xprod({<span class="number">1</span>,<span class="number">2</span>},{<span class="string">'a'</span>,<span class="string">'b'</span>})
<span class="comment">-- =&gt; {{1,'a'},{1,'b'},{2,'a'},{2,'b'}}</span>
</pre>


<h3>xpairs (value, array)</h3>

<p>Creates pairs from value and array. Value is always prepended to the pair.</p>


<pre>
<span class="keyword">local</span> t = M.xpairs(<span class="number">1</span>, {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>})
<span class="comment">-- =&gt; {{1,1},{1,2},{1,3}}</span>
</pre>


<h3>xpairsRight (value, array)</h3>

<p>Creates pairs from value and array. Value is always appended as the last item to the pair.</p>


<pre>
<span class="keyword">local</span> t = M.xpairsRight(<span class="number">1</span>, {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>})
<span class="comment">-- =&gt; {{1,1},{2,1},{3,1}}</span>
</pre>


<h3>sum (array)</h3>

<p>Returns the sum of array values.</p>


<pre>
M.sum({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>}) <span class="comment">-- =&gt; 15</span>
</pre>


<h3>product (array)</h3>

<p>Returns the product of array values.</p>


<pre>
M.product({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>}) <span class="comment">-- =&gt; 120</span>
</pre>


<h3>mean (array)</h3>

<p>Returns the mean of array values.</p>


<pre>
M.mean({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>}) <span class="comment">-- =&gt; 3</span>
</pre>


<h3>median (array)</h3>

<p>Returns the median of array values.</p>


<pre>
M.median({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>}) <span class="comment">-- =&gt; 3
</span>M.median({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>}) <span class="comment">-- =&gt; 2.5</span>
</pre>


<p><strong><a href="#TOC">[⬆]</a></strong></p>

<p><a name="_a_name__utility__Utility_functions__a_"></a></p>
<h2><a name='utility'>Utility functions</a></h2>

<h3>noop ()</h3>

<p>The no-operation function. Takes nothing, returns nothing. It is being used internally.</p>


<pre>
M.noop() <span class="comment">-- =&gt; nil</span>
</pre>


<h3>identity (value)</h3>

<p>Returns the passed-in value. <br/>
This function is internally used as a default transformation function.</p>


<pre>
M.identity(<span class="number">1</span>)<span class="comment">-- =&gt; 1
</span>M.identity(<span class="keyword">false</span>) <span class="comment">-- =&gt; false
</span>M.identity(<span class="string">'hello!'</span>) <span class="comment">-- =&gt; 'hello!'</span>
</pre>


<h3>call (f [, ...])</h3>

<p>Calls <code>f</code> with the supplied arguments. Returns the results of <code>f(...)</code>.</p>


<pre>
M.call(<span class="global">math</span>.pow, <span class="number">2</span>, <span class="number">3</span>) <span class="comment">-- =&gt; 8
</span>M.call(<span class="global">string</span>.len, <span class="string">'hello'</span> ) <span class="comment">-- =&gt; 5
</span>M.call(<span class="global">table</span>.concat, {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>}, <span class="string">','</span>, <span class="number">2</span>, <span class="number">4</span>) <span class="comment">-- =&gt; {2,3,4}</span>
</pre>


<h3>constant (value)</h3>

<p>Creates a constant function. This function will continuously yield the same output.</p>


<pre>
<span class="keyword">local</span> pi = M.constant(<span class="global">math</span>.pi)
pi(<span class="number">1</span>) <span class="comment">-- =&gt; 3.1415926535898
</span>pi(<span class="number">2</span>) <span class="comment">-- =&gt; 3.1415926535898
</span>pi(<span class="global">math</span>.pi) <span class="comment">-- =&gt; 3.1415926535898</span>
</pre>


<h3>applySpec (specs)</h3>

<p>Returns a function which applies <code>specs</code> on args. This function will produce an object having the same structure than <code>specs</code>
by mapping each property to the result of calling its associated function with the supplied arguments.</p>


<pre>
<span class="keyword">local</span> stats = M.applySpec({
  min = <span class="keyword">function</span>(...) <span class="keyword">return</span> <span class="global">math</span>.min(...) <span class="keyword">end</span>,
  max = <span class="keyword">function</span>(...) <span class="keyword">return</span> <span class="global">math</span>.max(...) <span class="keyword">end</span>,
})

stats(<span class="number">5</span>,<span class="number">4</span>,<span class="number">10</span>,<span class="number">1</span>,<span class="number">8</span>) <span class="comment">-- =&gt; {min = 1, max = 10}</span>
</pre>


<h3>thread (value [, ...])</h3>

<p>Threads <a href="index.html#obj:value">value</a> through a series of functions.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> inc(x) <span class="keyword">return</span> x + <span class="number">1</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> double(x) <span class="keyword">return</span> <span class="number">2</span> * x <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> square(x) <span class="keyword">return</span> x * x <span class="keyword">end</span>
M.thread(<span class="number">2</span>, inc, double, square) <span class="comment">-- =&gt; 36
</span>M.thread(<span class="number">3</span>, double, inc, square) <span class="comment">-- =&gt; 49
</span>M.thread(<span class="number">4</span>, square, double, inc) <span class="comment">-- =&gt; 33
</span>M.thread(<span class="number">5</span>, square, inc, double) <span class="comment">-- =&gt; 52</span>
</pre>


<p>If a function expects more than one args, it can be specified using an array list,
where the first item is the function and the following are the remaining args neeeded. </p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> inc(x) <span class="keyword">return</span> x + <span class="number">1</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> add(x, y) <span class="keyword">return</span> x * y <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> pow(x, y) <span class="keyword">return</span> x ^ y <span class="keyword">end</span>
M.thread(<span class="number">2</span>, inc, {add, <span class="number">3</span>}, {pow, <span class="number">2</span>}) <span class="comment">-- =&gt; 36
</span>M.thread(<span class="number">2</span>, {add, <span class="number">4</span>}, inc, {pow, <span class="number">2</span>}) <span class="comment">-- =&gt; 49</span>
</pre>


<h3>threadRight (value [, ...])</h3>

<p>Threads <a href="index.html#obj:value">value</a> through a series of functions. If a function expects more than one args,
it can be specified using an array list, where the first item is the function and the following are
the remaining args neeeded. The value is used as the last input.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> inc(x) <span class="keyword">return</span> x + <span class="number">1</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> add(x, y) <span class="keyword">return</span> x * y <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> pow(x, y) <span class="keyword">return</span> x ^ y <span class="keyword">end</span>
M.threadRight(<span class="number">2</span>, inc, {add, <span class="number">3</span>}, {pow, <span class="number">2</span>}) <span class="comment">-- =&gt; 64
</span>M.threadRight(<span class="number">2</span>, {add, <span class="number">4</span>}, inc, {pow, <span class="number">2</span>}) <span class="comment">-- =&gt; 128</span>
</pre>


<h3>dispatch (...)</h3>

<p>Returns a dispatching function. When called with arguments, this function invokes each of its functions
in the passed-in order and returns the results of the first non-nil evaluation.</p>


<pre>
<span class="keyword">local</span> f = M.dispatch(
  <span class="keyword">function</span>() <span class="keyword">return</span> <span class="keyword">nil</span> <span class="keyword">end</span>,
  <span class="keyword">function</span> (v) <span class="keyword">return</span> v+<span class="number">1</span> <span class="keyword">end</span>,
  <span class="keyword">function</span> (v) <span class="keyword">return</span> <span class="number">2</span>*v <span class="keyword">end</span>
)
f(<span class="number">5</span>) <span class="comment">-- =&gt; 6
</span>f(<span class="number">7</span>) <span class="comment">-- =&gt; 8</span>
</pre>


<h3>memoize (f)</h3>
<p>*Aliases: <code>cache</code>*.</p>

<p>Memoizes a slow-running function. It caches the result for a specific input, so that the next time the function is called with the same input, it will lookup the result in its cache, instead of running again the function body.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> fibonacci(n)
  <span class="keyword">return</span> n &lt; <span class="number">2</span> <span class="keyword">and</span> n <span class="keyword">or</span> fibonacci(n-<span class="number">1</span>)+fibonacci(n-<span class="number">2</span>)
<span class="keyword">end</span>
<span class="keyword">local</span> mem_fibonacci = M.memoize(fibonacci)
fibonacci(<span class="number">20</span>) <span class="comment">-- =&gt; 6765 (but takes some time)
</span>mem_fibonacci(<span class="number">20</span>) <span class="comment">-- =&gt; 6765 (takes less time)</span>
</pre>


<h3>unfold (f, seed)</h3>

<p>Builds a list from a seed value. Accepts an iterator function, which returns either nil to stop iteration or two values : the value to add to the list of results and the seed to be used in the next call to the iterator function.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> f(v)
  <span class="keyword">if</span> v &lt; <span class="number">100</span> <span class="keyword">then</span> <span class="keyword">return</span> v, v * <span class="number">2</span> <span class="keyword">end</span>
<span class="keyword">end</span>
<span class="keyword">local</span> t = M.unfold(f, <span class="number">10</span>) <span class="comment">-- =&gt; {10,20,40,80}</span>
</pre>


<h3>once (f)</h3>

<p>Produces a function that runs only once. Successive calls to this function will still yield the same input.</p>


<pre>
<span class="keyword">local</span> sq = M.once(<span class="keyword">function</span>(a) <span class="keyword">return</span> a*a <span class="keyword">end</span>)
sq(<span class="number">1</span>) <span class="comment">-- =&gt; 1
</span>sq(<span class="number">2</span>) <span class="comment">-- =&gt; 1
</span>sq(<span class="number">3</span>) <span class="comment">-- =&gt; 1
</span>sq(<span class="number">4</span>) <span class="comment">-- =&gt; 1
</span>sq(<span class="number">5</span>) <span class="comment">-- =&gt; 1</span>
</pre>


<h3>before (f, count)</h3>

<p>Returns a version of <code>f</code> that will run no more than <a href="index.html#count">count</a> times. Next calls will keep yielding the results of the (n-th)-1 call.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> greet(someone) <span class="keyword">return</span> <span class="string">'hello '</span>..someone <span class="keyword">end</span>
<span class="keyword">local</span> greetOnly3people = M.before(greet, <span class="number">3</span>)
greetOnly3people(<span class="string">'John'</span>) <span class="comment">-- =&gt; 'hello John'
</span>greetOnly3people(<span class="string">'Moe'</span>) <span class="comment">-- =&gt; 'hello Moe'
</span>greetOnly3people(<span class="string">'James'</span>) <span class="comment">-- =&gt; 'hello James'
</span>greetOnly3people(<span class="string">'Joseph'</span>) <span class="comment">-- =&gt; 'hello James'
</span>greetOnly3people(<span class="string">'Allan'</span>) <span class="comment">-- =&gt; 'hello James'</span>
</pre>


<h3>after (f, count)</h3>

<p>Produces a function that will respond only after a given number of calls.</p>


<pre>
<span class="keyword">local</span> f = M.after(M.identity,<span class="number">3</span>)
f(<span class="number">1</span>) <span class="comment">-- =&gt; nil
</span>f(<span class="number">2</span>) <span class="comment">-- =&gt; nil
</span>f(<span class="number">3</span>) <span class="comment">-- =&gt; 3
</span>f(<span class="number">4</span>) <span class="comment">-- =&gt; 4</span>
</pre>


<h3>compose (...)</h3>

<p>Composes functions. Each function consumes the return value of the one that follows.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> f(x) <span class="keyword">return</span> x^<span class="number">2</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> g(x) <span class="keyword">return</span> x+<span class="number">1</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> h(x) <span class="keyword">return</span> x/<span class="number">2</span> <span class="keyword">end</span>
<span class="keyword">local</span> compositae = M.compose(f,g,h)
compositae(<span class="number">10</span>) <span class="comment">-- =&gt; 36
</span>compositae(<span class="number">20</span>) <span class="comment">-- =&gt; 121</span>
</pre>


<h3>pipe (value, ...)</h3>

<p>Pipes a value through a series of functions.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> f(x) <span class="keyword">return</span> x^<span class="number">2</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> g(x) <span class="keyword">return</span> x+<span class="number">1</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> h(x) <span class="keyword">return</span> x/<span class="number">2</span> <span class="keyword">end</span>
M.pipe(<span class="number">10</span>,f,g,h) <span class="comment">-- =&gt; 36
</span>M.pipe(<span class="number">20</span>,f,g,h) <span class="comment">-- =&gt; 121</span>
</pre>


<h3>complement (f)</h3>

<p>Returns a function which returns the logical complement of a given function.</p>


<pre>
M.complement(<span class="keyword">function</span>() <span class="keyword">return</span> <span class="keyword">true</span> <span class="keyword">end</span>)() <span class="comment">-- =&gt; false</span>
</pre>


<h3>juxtapose (value, ...)</h3>
<p>*Aliases: <code>juxt</code>*.</p>

<p>Calls a sequence of functions with the same input.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> f(x) <span class="keyword">return</span> x^<span class="number">2</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> g(x) <span class="keyword">return</span> x+<span class="number">1</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> h(x) <span class="keyword">return</span> x/<span class="number">2</span> <span class="keyword">end</span>
M.juxtapose(<span class="number">10</span>, f, g, h) <span class="comment">-- =&gt; 100, 11, 5</span>
</pre>


<h3>wrap (f, wrapper)</h3>

<p>Wraps a function inside a wrapper. Allows the wrapper to execute code before and after function run.</p>


<pre>
<span class="keyword">local</span> greet = <span class="keyword">function</span>(name) <span class="keyword">return</span> <span class="string">"hi: "</span> .. name <span class="keyword">end</span>
<span class="keyword">local</span> greet_backwards = M.wrap(greet, <span class="keyword">function</span>(f,arg)
  <span class="keyword">return</span> f(arg) ..<span class="string">'\nhi: '</span> .. arg:reverse()
<span class="keyword">end</span>)
greet_backwards(<span class="string">'John'</span>)

<span class="comment">-- =&gt; hi: John
</span><span class="comment">-- =&gt; hi: nhoJ</span>
</pre>


<h3>times (iter [, n])</h3>

<p>Calls a given function <code>n</code> times.</p>


<pre>
<span class="keyword">local</span> f = (<span class="string">'Lua programming'</span>):gmatch(<span class="string">'.'</span>)
M.times(f, <span class="number">3</span>) <span class="comment">-- =&gt; {'L','u','a'}</span>
</pre>


<h3>bind (f, v)</h3>

<p>Binds a value to be the first argument to a function.</p>


<pre>
<span class="keyword">local</span> sqrt2 = M.bind(<span class="global">math</span>.sqrt,<span class="number">2</span>)
sqrt2() <span class="comment">-- =&gt; 1.4142135623731</span>
</pre>


<h3>bind2 (f, v)</h3>

<p>Binds a value to be the second argument to a function.</p>


<pre>
<span class="keyword">local</span> last2 = M.bind(M.last,<span class="number">2</span>)
last2({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>}) <span class="comment">-- =&gt; {5,6}</span>
</pre>


<h3>bindn (f, ...)</h3>

<p>Binds a variable number of values to be the first arguments to a function.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> out(...) <span class="keyword">return</span> <span class="global">table</span>.concat {...} <span class="keyword">end</span>
<span class="keyword">local</span> out = M.bindn(out,<span class="string">'OutPut'</span>,<span class="string">':'</span>,<span class="string">' '</span>)
out(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>) <span class="comment">-- =&gt; OutPut: 123
</span>out(<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>) <span class="comment">-- =&gt; OutPut: abcd</span>
</pre>


<h3>bindall (obj, ...)</h3>

<p>Binds methods to object. As such, when calling any of these methods, they will receive object as a first argument.</p>


<pre>
<span class="keyword">local</span> window = {
    setPos = <span class="keyword">function</span>(w,x,y) w.x, w.y = x, y <span class="keyword">end</span>,
    setName = <span class="keyword">function</span>(w,name) w.name = name <span class="keyword">end</span>,
    getName = <span class="keyword">function</span>(w) <span class="keyword">return</span> w.name <span class="keyword">end</span>,
}
window = M.bindall(window, <span class="string">'setPos'</span>, <span class="string">'setName'</span>, <span class="string">'getName'</span>)
window.setPos(<span class="number">10</span>,<span class="number">15</span>)
<span class="global">print</span>(window.x, window.y) <span class="comment">-- =&gt; 10,15
</span>
window.setName(<span class="string">'fooApp'</span>)
<span class="global">print</span>(window.name) <span class="comment">-- =&gt; 'fooApp'
</span>
<span class="global">print</span>(window.getName()) <span class="comment">-- =&gt; 'fooApp'</span>
</pre>


<h3>cond (conds)</h3>

<p>Returns a function which iterate over an array list of conditions. It invokes each predicate, passing it given values. It returns the value of the corresponding function of the first predicate to return a non-nil value</p>


<pre>
<span class="keyword">local</span> multipleOf = M.cond({
  {<span class="keyword">function</span>(v) <span class="keyword">return</span> v%<span class="number">2</span>==<span class="number">0</span> <span class="keyword">end</span>, <span class="keyword">function</span>(v) <span class="keyword">return</span> v..<span class="string">' is multiple of 2'</span> <span class="keyword">end</span>},
  {<span class="keyword">function</span>(v) <span class="keyword">return</span> v%<span class="number">3</span>==<span class="number">0</span> <span class="keyword">end</span>, <span class="keyword">function</span>(v) <span class="keyword">return</span> v..<span class="string">' is multiple of 3'</span> <span class="keyword">end</span>},
  {<span class="keyword">function</span>(v) <span class="keyword">return</span> v%<span class="number">5</span>==<span class="number">0</span> <span class="keyword">end</span>, <span class="keyword">function</span>(v) <span class="keyword">return</span> v..<span class="string">' is multiple of 5'</span> <span class="keyword">end</span>},
  {<span class="keyword">function</span>() <span class="keyword">return</span> <span class="keyword">true</span> <span class="keyword">end</span>, <span class="keyword">function</span>(v) <span class="keyword">return</span> <span class="string">'could not find an answer for '</span>..v <span class="keyword">end</span>}
})
<span class="keyword">for</span> i = <span class="number">15</span>, <span class="number">20</span> <span class="keyword">do</span>
  <span class="global">print</span>(multipleOf(i))
<span class="keyword">end</span>

<span class="comment">-- =&gt; 15 is multiple of 3
</span><span class="comment">-- =&gt; 16 is multiple of 2
</span><span class="comment">-- =&gt; could not find an answer for 17
</span><span class="comment">-- =&gt; 18 is multiple of 2
</span><span class="comment">-- =&gt; could not find an answer for 19
</span><span class="comment">-- =&gt; 20 is multiple of 2</span>
</pre>


<h3>both (...)</h3>

<p>Returns a validation function. Given a set of functions, the validation function
evaluates to <code>true</code> only when all its funcs returns <code>true</code>.</p>


<pre>
<span class="keyword">local</span> f = M.both(
    <span class="keyword">function</span>(x) <span class="keyword">return</span> x &gt; <span class="number">0</span> <span class="keyword">end</span>,
    <span class="keyword">function</span>(x) <span class="keyword">return</span> x &lt; <span class="number">10</span> <span class="keyword">end</span>,
    <span class="keyword">function</span>(x) <span class="keyword">return</span> x % <span class="number">2</span> == <span class="number">0</span> <span class="keyword">end</span>
)
f(<span class="number">2</span>) <span class="comment">-- =&gt; true
</span>f(<span class="number">8</span>) <span class="comment">-- =&gt; true
</span>f(<span class="number">9</span>) <span class="comment">-- =&gt; false</span>
</pre>


<h3>either (...)</h3>

<p>Returns a validation function. Given a set of functions, the validation function
evaluates to <code>true</code> when one of its funcs returns <code>true</code>.</p>


<pre>
<span class="keyword">local</span> f = M.either(
    <span class="keyword">function</span>(x) <span class="keyword">return</span> x &gt; <span class="number">0</span> <span class="keyword">end</span>,
    <span class="keyword">function</span>(x) <span class="keyword">return</span> x % <span class="number">2</span> == <span class="number">0</span> <span class="keyword">end</span>
)
f(<span class="number">0</span>) <span class="comment">-- =&gt; true
</span>f(-<span class="number">3</span>) <span class="comment">-- =&gt; false</span>
</pre>


<h3>neither (...)</h3>

<p>Returns a validation function. Given a set of functions, the validation function
evaluates to <code>true</code> when neither of its funcs returns <code>true</code>.</p>


<pre>
<span class="keyword">local</span> f = M.neither(
    <span class="keyword">function</span>(x) <span class="keyword">return</span> x &gt; <span class="number">10</span> <span class="keyword">end</span>,
    <span class="keyword">function</span>(x) <span class="keyword">return</span> x % <span class="number">2</span> == <span class="number">0</span> <span class="keyword">end</span>
)
f(<span class="number">12</span>) <span class="comment">-- =&gt; false
</span>f(<span class="number">8</span>) <span class="comment">-- =&gt; false
</span>f(<span class="number">7</span>) <span class="comment">-- =&gt; true</span>
</pre>


<h3>uniqueId ([template])</h3>
<p>*Aliases: <code>uid</code>*.</p>

<p>Returns an unique integer ID.</p>


<pre>
M.uniqueId() <span class="comment">-- =&gt; 1</span>
</pre>


<p>Can handle string templates for formatted output.</p>


<pre>
M.uniqueId(<span class="string">'ID%s'</span>) <span class="comment">-- =&gt; 'ID2'</span>
</pre>


<p>Or a function, for the same purpose.</p>


<pre>
<span class="keyword">local</span> formatter = <span class="keyword">function</span>(ID) <span class="keyword">return</span> <span class="string">'$'</span>..ID..<span class="string">'$'</span> <span class="keyword">end</span>
M.uniqueId(formatter) <span class="comment">-- =&gt; '$ID1$'</span>
</pre>


<h3>iterator (f, value [, n])</h3>
<p>*Aliases: <code>iter</code>*.</p>

<p>Returns an iterator function which constinuously applies a function <code>f</code> onto an input <a href="index.html#obj:value">value</a>.
For example, let us go through the powers of two using <a href="index.html#iterator">iterator</a>.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> po2(x) <span class="keyword">return</span> x*<span class="number">2</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> iter_po2 = M.iterator(po2, <span class="number">1</span>)
iter_po2() <span class="comment">-- =&gt; 2
</span>iter_po2() <span class="comment">-- =&gt; 4
</span>iter_po2() <span class="comment">-- =&gt; 8</span>
</pre>


<p>if <code>n</code> is supplied, it will run at maximum <code>n</code> times.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> po2(x) <span class="keyword">return</span> x*<span class="number">2</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> iter_po2 = M.iterator(po2, <span class="number">1</span>, <span class="number">3</span>)
iter_po2() <span class="comment">-- =&gt; 2
</span>iter_po2() <span class="comment">-- =&gt; 4
</span>iter_po2() <span class="comment">-- =&gt; 8
</span>iter_po2() <span class="comment">-- =&gt; nil</span>
</pre>


<h3>skip (iter [, n = 1])</h3>

<p>Consumes the first <code>n</code> values of a iterator then returns it.</p>


<pre>
<span class="keyword">local</span> w = <span class="string">"hello"</span>
<span class="keyword">local</span> char = <span class="global">string</span>.gmatch(w,<span class="string">'.'</span>)
<span class="keyword">local</span> iter = M.skip(char, <span class="number">3</span>)
<span class="keyword">for</span> w <span class="keyword">in</span> iter <span class="keyword">do</span> <span class="global">print</span>(w) <span class="keyword">end</span> <span class="comment">-- =&gt; 'l', 'o'</span>
</pre>


<p><code>n</code> defaults to 1 when not given.</p>


<pre>
<span class="keyword">local</span> w = <span class="string">"hello"</span>
<span class="keyword">local</span> char = <span class="global">string</span>.gmatch(w,<span class="string">'.'</span>)
<span class="keyword">local</span> iter = M.skip(char)
<span class="keyword">for</span> w <span class="keyword">in</span> iter <span class="keyword">do</span> <span class="global">print</span>(w) <span class="keyword">end</span> <span class="comment">-- =&gt; 'e', 'l', 'l', 'o'</span>
</pre>


<h3>tabulate (...)</h3>

<p>Iterates a given iterator function and returns its values packed in an array.</p>


<pre>
<span class="keyword">local</span> text = <span class="string">'letters'</span>
<span class="keyword">local</span> chars = <span class="global">string</span>.gmatch(text, <span class="string">'.'</span>)
M.tabulate(chars) <span class="comment">-- =&gt; {'l','e','t','t','e','r','s'}</span>
</pre>


<h3>iterlen (...)</h3>

<p>Returns the length of an iterator.</p>


<pre>
<span class="keyword">local</span> text = <span class="string">'letters'</span>
<span class="keyword">local</span> chars = <span class="global">string</span>.gmatch(text, <span class="string">'.'</span>)
M.iterlen(chars) <span class="comment">-- =&gt; 7</span>
</pre>


<p>It consumes the iterator itself.</p>


<pre>
<span class="keyword">local</span> text = <span class="string">'lua'</span>
<span class="keyword">local</span> chars = <span class="global">string</span>.gmatch(text, <span class="string">'.'</span>)
M.iterlen(chars) <span class="comment">-- =&gt; 3
</span>chars() <span class="comment">-- =&gt; nil</span>
</pre>


<h3>castArray (value)</h3>

<p>Casts the passed-in value to an array containing the value itself.</p>


<pre>
M.castArray(<span class="keyword">true</span>) <span class="comment">-- =&gt; {true}
</span>M.castArray(<span class="number">2</span>) <span class="comment">-- =&gt; {2}</span>
</pre>


<p>It leaves the given value untouched in case it is already a table.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>}
<span class="global">print</span>(M.castArray(t) == t) <span class="comment">-- =&gt; true</span>
</pre>


<h3>flip (f)</h3>

<p>Creates a function of <code>f</code> with arguments flipped in reverse order.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> f(...) <span class="keyword">return</span> <span class="global">table</span>.concat({...}) <span class="keyword">end</span>
<span class="keyword">local</span> flipped = M.flip(f)
flipped(<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>) <span class="comment">-- =&gt; 'cba'</span>
</pre>


<h3>nthArg (n)</h3>

<p>Returns a function that gets the nth argument. </p>


<pre>
<span class="keyword">local</span> f = M.nthArg(<span class="number">3</span>)
f(<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>) <span class="comment">-- =&gt; 'c'</span>
</pre>


<p>If n is negative, the nth argument from the end is returned.</p>


<pre>
<span class="keyword">local</span> f = M.nthArg(-<span class="number">2</span>)
f(<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>) <span class="comment">-- =&gt; 'b'</span>
</pre>


<h3>unary (f)</h3>

<p>Returns a function which accepts up to one argument. It ignores any additional arguments. </p>


<pre>
<span class="keyword">local</span> f = M.unary(<span class="keyword">function</span> (...) <span class="keyword">return</span> ... <span class="keyword">end</span>)
f(<span class="string">'a'</span>) - ==&gt; <span class="string">'a'</span>
f(<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>) <span class="comment">-- =&gt; 'a'</span>
</pre>


<h3>ary (f [, n = 1])</h3>
<p>*Aliases: <code>nAry</code>*.</p>

<p>Returns a function which accepts up to <code>n</code> args. It ignores any additional arguments.</p>


<pre>
<span class="keyword">local</span> f = M.ary(<span class="keyword">function</span> (...) <span class="keyword">return</span> ... <span class="keyword">end</span>, <span class="number">2</span>)
f(<span class="number">1</span>,<span class="number">2</span>) - ==&gt; <span class="number">1</span>,<span class="number">2</span>
f(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>) <span class="comment">-- =&gt; 1,2</span>
</pre>


<p>If <code>n</code> is not given, it defaults to <code>1</code>.</p>


<pre>
<span class="keyword">local</span> f = M.unary(<span class="keyword">function</span> (...) <span class="keyword">return</span> ... <span class="keyword">end</span>)
f(<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>) <span class="comment">-- =&gt; 'a'</span>
</pre>


<h3>noarg (f)</h3>

<p>Returns a function with an arity of 0. The new function ignores any arguments passed to it.</p>


<pre>
<span class="keyword">local</span> f = M.noarg(<span class="keyword">function</span> (x) <span class="keyword">return</span> x <span class="keyword">or</span> <span class="string">'default'</span> <span class="keyword">end</span>)
f(<span class="number">1</span>) <span class="comment">-- =&gt; 'default'
</span>f(<span class="keyword">function</span>() <span class="keyword">end</span>, <span class="number">3</span>) <span class="comment">-- =&gt; 'default'</span>
</pre>


<h3>rearg (f, indexes)</h3>

<p>Returns a function which runs with arguments arranged according to given <code>indexes</code>.</p>


<pre>
<span class="keyword">local</span> f = M.rearg(<span class="keyword">function</span> (...) <span class="keyword">return</span> ... <span class="keyword">end</span>, {<span class="number">5</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">1</span>})
f(<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>,<span class="string">'e'</span>) <span class="comment">-- =&gt; 'e','d','c','b','a'</span>
</pre>


<h3>over (...)</h3>

<p>Creates a function that invokes a set of transforms with the arguments it receives.<br/>
One can use use for example to get the tuple of min and max values from a set of values</p>


<pre>
<span class="keyword">local</span> minmax = M.over(<span class="global">math</span>.min, <span class="global">math</span>.max)
minmax(<span class="number">5</span>,<span class="number">10</span>,<span class="number">12</span>,<span class="number">4</span>,<span class="number">3</span>) <span class="comment">-- =&gt; {3,12}</span>
</pre>


<h3>overEvery (...)</h3>

<p>Creates a validation function. The returned function checks if all of the given predicates return truthy when invoked with the arguments it receives.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> alleven(...)
    <span class="keyword">for</span> i, v <span class="keyword">in</span> <span class="global">ipairs</span>({...}) <span class="keyword">do</span>
        <span class="keyword">if</span> v%<span class="number">2</span>~=<span class="number">0</span> <span class="keyword">then</span> <span class="keyword">return</span> <span class="keyword">false</span> <span class="keyword">end</span>
    <span class="keyword">end</span>
    <span class="keyword">return</span> <span class="keyword">true</span>
<span class="keyword">end</span>

<span class="keyword">local</span> <span class="keyword">function</span> allpositive(...)
    <span class="keyword">for</span> i, v <span class="keyword">in</span> <span class="global">ipairs</span>({...}) <span class="keyword">do</span>
        <span class="keyword">if</span> v &lt; <span class="number">0</span> <span class="keyword">then</span> <span class="keyword">return</span> <span class="keyword">false</span> <span class="keyword">end</span>
    <span class="keyword">end</span>
    <span class="keyword">return</span> <span class="keyword">true</span>
<span class="keyword">end</span>

<span class="keyword">local</span> allok = M.overEvery(alleven, allpositive)

allok(<span class="number">2</span>,<span class="number">4</span>,-<span class="number">1</span>,<span class="number">8</span>) <span class="comment">-- =&gt; false
</span>allok(<span class="number">10</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">6</span>) <span class="comment">-- =&gt; false
</span>allok(<span class="number">8</span>,<span class="number">4</span>,<span class="number">6</span>,<span class="number">10</span>) <span class="comment">-- =&gt; true</span>
</pre>


<h3>overSome (...)</h3>

<p>Creates a validation function. The returned function checks if any of the given predicates return truthy when invoked with the arguments it receives.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> alleven(...)
    <span class="keyword">for</span> i, v <span class="keyword">in</span> <span class="global">ipairs</span>({...}) <span class="keyword">do</span>
        <span class="keyword">if</span> v%<span class="number">2</span>~=<span class="number">0</span> <span class="keyword">then</span> <span class="keyword">return</span> <span class="keyword">false</span> <span class="keyword">end</span>
    <span class="keyword">end</span>
    <span class="keyword">return</span> <span class="keyword">true</span>
<span class="keyword">end</span>

<span class="keyword">local</span> <span class="keyword">function</span> allpositive(...)
    <span class="keyword">for</span> i, v <span class="keyword">in</span> <span class="global">ipairs</span>({...}) <span class="keyword">do</span>
        <span class="keyword">if</span> v &lt; <span class="number">0</span> <span class="keyword">then</span> <span class="keyword">return</span> <span class="keyword">false</span> <span class="keyword">end</span>
    <span class="keyword">end</span>
    <span class="keyword">return</span> <span class="keyword">true</span>
<span class="keyword">end</span>

<span class="keyword">local</span> anyok = M.overSome(alleven,allpositive)

anyok(<span class="number">2</span>,<span class="number">4</span>,-<span class="number">1</span>,<span class="number">8</span>) <span class="comment">-- =&gt; false
</span>anyok(<span class="number">10</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">6</span>) <span class="comment">-- =&gt; true
</span>anyok(-<span class="number">1</span>,-<span class="number">5</span>,-<span class="number">3</span>) <span class="comment">-- =&gt; false</span>
</pre>


<h3>overArgs (f, ...)</h3>

<p>Creates a function that invokes <code>f</code> with its arguments transformed</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> f(x, y) <span class="keyword">return</span> x, y <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> triple(x) retun x*<span class="number">3</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> square(x) retun x^<span class="number">2</span> <span class="keyword">end</span>
<span class="keyword">local</span> new_f = M.overArgs(f, triple, square)

new_f(<span class="number">1</span>,<span class="number">2</span>) <span class="comment">-- =&gt; 3, 4
</span>new_f(<span class="number">10</span>,<span class="number">10</span>) <span class="comment">-- =&gt; 30, 100</span>
</pre>


<p>In case the number of arguments is greater than the number of transforms, the remaining args will be left as-is.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> f(x, y, z) <span class="keyword">return</span> x, y, z <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> triple(x) retun x*<span class="number">3</span> <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> square(x) retun x^<span class="number">2</span> <span class="keyword">end</span>
<span class="keyword">local</span> new_f = M.overArgs(f, triple, square)

new_f(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>) <span class="comment">-- =&gt; 3, 4, 3
</span>new_f(<span class="number">10</span>,<span class="number">10</span>,<span class="number">10</span>) <span class="comment">-- =&gt; 30, 100, 10</span>
</pre>


<h3>converge (f, g, h)</h3>

<p>Converges two functions into one.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> pow2(x) <span class="keyword">return</span> x*x <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> pow3(x) <span class="keyword">return</span> x*x*x <span class="keyword">end</span>
<span class="keyword">local</span> <span class="keyword">function</span> sum(a,b) <span class="keyword">return</span> a+b <span class="keyword">end</span>
<span class="keyword">local</span> poly = M.converge(sum, pow2, pow3)
poly(<span class="number">5</span>) <span class="comment">-- =&gt; 150 (ie. 5*5 + 5*5*5)</span>
</pre>


<h3>partial (f, ...)</h3>

<p>Partially apply a function by filling in any number of its arguments. </p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> diff(a, b) <span class="keyword">return</span> a - b <span class="keyword">end</span>
<span class="keyword">local</span> diffFrom20 = M.partial(diff, <span class="number">20</span>) <span class="comment">-- arg 'a' will be 20 by default
</span>diffFrom20(<span class="number">5</span>) <span class="comment">-- =&gt; 15</span>
</pre>


<p>The string <code>&apos;_&apos;</code> can be used as a placeholder in the list of arguments to specify an argument that should not be pre-filled, but is rather left open to be supplied at call-time.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> diff(a, b) <span class="keyword">return</span> a - b <span class="keyword">end</span>
<span class="keyword">local</span> remove5 = M.partial(diff, <span class="string">'_'</span>, <span class="number">5</span>) <span class="comment">-- arg 'a' will be given at call-time, but 'b' is set to 5
</span>remove5(<span class="number">20</span>) <span class="comment">-- =&gt; 15</span>
</pre>


<h3>partialRight (f, ...)</h3>

<p>Like <code>M.partial</code>, it partially applies a function by filling in any number of its arguments, but from the right.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> concat(...) <span class="keyword">return</span> <span class="global">table</span>.concat({...},<span class="string">','</span>) <span class="keyword">end</span>
<span class="keyword">local</span> concat_right = M.partialRight(concat,<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>)
concat_right(<span class="string">'d'</span>) <span class="comment">-- =&gt; d,a,b,c
</span>
concat_right = M.partialRight(concat,<span class="string">'a'</span>,<span class="string">'b'</span>)
concat_right(<span class="string">'c'</span>,<span class="string">'d'</span>) <span class="comment">-- =&gt; c,d,a,b
</span>
concat_right = M.partialRight(concat,<span class="string">'a'</span>)
concat_right(<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>) <span class="comment">-- =&gt; b,c,d,a</span>
</pre>


<p>The string <code>&apos;_&apos;</code>, as always, can be used as a placeholder in the list of arguments to specify an argument that should not be pre-filled, but is rather left open to be supplied at call-time.
In that case, the first args supplied at runtime will be used to fill the initial list of args while the remaining will be prepended.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> concat(...) <span class="keyword">return</span> <span class="global">table</span>.concat({...},<span class="string">','</span>) <span class="keyword">end</span>
<span class="keyword">local</span> concat_right = M.partialRight(concat,<span class="string">'a'</span>,<span class="string">'_'</span>,<span class="string">'c'</span>)
concat_right(<span class="string">'d'</span>,<span class="string">'b'</span>) <span class="comment">-- =&gt; b,a,d,c
</span>
concat_right = M.partialRight(concat,<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'_'</span>)
concat_right(<span class="string">'c'</span>,<span class="string">'d'</span>) <span class="comment">-- =&gt; d,a,b,c
</span>
concat_right = M.partialRight(concat,<span class="string">'_'</span>,<span class="string">'a'</span>)
concat_right(<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'d'</span>) <span class="comment">-- =&gt; c,d,b,a</span>
</pre>


<h3>curry (f [, n_args = 2])</h3>

<p>Curries a function. If the given function <code>f</code> takes multiple arguments, it returns another version of <code>f</code> that takes a single argument
(the first of the arguments to the original function) and returns a new function that takes the remainder of the arguments and returns the result.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> sumOf3args(x,y,z) <span class="keyword">return</span> x + y + z <span class="keyword">end</span>
<span class="keyword">local</span> curried_sumOf3args = M.curry(sumOf3args, <span class="number">3</span>)
sumOf3args(<span class="number">1</span>)(<span class="number">2</span>)(<span class="number">3</span>)) <span class="comment">-- =&gt; 6
</span>sumOf3args(<span class="number">0</span>)(<span class="number">6</span>)(<span class="number">9</span>)) <span class="comment">-- =&gt; 15</span>
</pre>


<p><code>n_args</code> defaults to 2.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> product(x,y) <span class="keyword">return</span> x * y <span class="keyword">end</span>
<span class="keyword">local</span> curried_product = M.curry(product)
curried_product(<span class="number">5</span>)(<span class="number">4</span>) <span class="comment">-- =&gt; 20
</span>curried_product(<span class="number">3</span>)(-<span class="number">5</span>) <span class="comment">-- =&gt; -15
</span>curried_product(<span class="number">0</span>)(<span class="number">1</span>) <span class="comment">-- =&gt; 0</span>
</pre>


<h3>time (f [, ...])</h3>

<p>Returns the execution time of <code>f (...)</code> in seconds and its results.</p>


<pre>
<span class="keyword">local</span> <span class="keyword">function</span> wait_count(n)
    <span class="keyword">local</span> i = <span class="number">0</span>
    <span class="keyword">while</span> i &lt; n <span class="keyword">do</span> i = i + <span class="number">1</span> <span class="keyword">end</span>
    <span class="keyword">return</span> i
<span class="keyword">end</span>

<span class="keyword">local</span> time, i = M.time(wait_count, <span class="number">1e6</span>) <span class="comment">-- =&gt; 0.002 1000000
</span><span class="keyword">local</span> time, i = M.time(wait_count, <span class="number">1e7</span>) <span class="comment">-- =&gt; 0.018 10000000</span>
</pre>


<p><strong><a href="#TOC">[⬆]</a></strong></p>

<p><a name="_a_name__object__Object_functions__a_"></a></p>
<h2><a name='object'>Object functions</a></h2>

<h3>keys (obj)</h3>

<p>Collects the names of an object attributes.</p>


<pre>
M.keys({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}) <span class="comment">-- =&gt; "{1,2,3}"
</span>M.keys({x = <span class="number">0</span>, y = <span class="number">1</span>}) <span class="comment">-- =&gt; "{'y','x'}"</span>
</pre>


<h3>values (obj)</h3>

<p>Collects the values of an object attributes.</p>


<pre>
M.values({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}) <span class="comment">-- =&gt; "{1,2,3}"
</span>M.values({x = <span class="number">0</span>, y = <span class="number">1</span>}) <span class="comment">-- =&gt; "{1,0}"</span>
</pre>


<h3>path (obj, ...)</h3>

<p>Returns the value at a given path in an object.</p>


<pre>
<span class="keyword">local</span> entity = {
  pos = {x = <span class="number">1</span>, y = <span class="number">2</span>},
  engine = {
    left = {status = <span class="string">'active'</span>, damage = <span class="number">5</span>},
    right = {status = <span class="string">'off'</span>, damage = <span class="number">10</span>}
  },
  boost = <span class="keyword">false</span>
}

M.path(entity,<span class="string">'pos'</span>,<span class="string">'x'</span>) <span class="comment">-- =&gt; 1
</span>M.path(entity,<span class="string">'pos'</span>,<span class="string">'y'</span>) <span class="comment">-- =&gt; 2
</span>M.path(entity,<span class="string">'engine'</span>,<span class="string">'left'</span>,<span class="string">'status'</span>) <span class="comment">-- =&gt; 'active'
</span>M.path(entity,<span class="string">'engine'</span>,<span class="string">'right'</span>,<span class="string">'damage'</span>) <span class="comment">-- =&gt; 10
</span>M.path(entity,<span class="string">'boost'</span>) <span class="comment">-- =&gt; false</span>
</pre>


<h3>spreadPath (obj, ...)</h3>

<p>Spreads object under property path onto provided object. It is similar to <a href="index.html#flattenPath">flattenPath</a>, but removes object under the property path.</p>


<pre>
<span class="keyword">local</span> obj = {a = <span class="number">1</span>, b = <span class="number">2</span>, c = {d = <span class="number">3</span>, e = <span class="number">4</span>, f = {g = <span class="number">5</span>}}}
M.spreadPath(obj, <span class="string">'c'</span>, <span class="string">'f'</span>)
<span class="comment">-- =&gt; {a = 1, b = 2, d = 3, e = 4, g = 5, c = {f = {}}}</span>
</pre>


<h3>flattenPath (obj, ...)</h3>

<p>Flattens object under property path onto provided object. It is similar to <a href="index.html#spreadPath">spreadPath</a>, but preserves object under the property path.</p>


<pre>
<span class="keyword">local</span> obj = {a = <span class="number">1</span>, b = <span class="number">2</span>, c = {d = <span class="number">3</span>, e = <span class="number">4</span>, f = {g = <span class="number">5</span>}}}
M.spreadPath(obj, <span class="string">'c'</span>, <span class="string">'f'</span>)
<span class="comment">-- =&gt; {a = 1, b = 2, d = 3, e = 4, g = 5, c = {d = 3, e = 4, f = {g = 5}}}</span>
</pre>


<h3>kvpairs (obj)</h3>

<p>Converts an object to an array-list of key-value pairs.</p>


<pre>
<span class="keyword">local</span> obj = {x = <span class="number">1</span>, y = <span class="number">2</span>, z = <span class="number">3</span>}
M.each(M.kvpairs(obj), <span class="keyword">function</span>(v,k)
    <span class="global">print</span>(k, <span class="global">table</span>.concat(v,<span class="string">','</span>))
<span class="keyword">end</span>)

<span class="comment">-- =&gt; 1    y,2
</span><span class="comment">-- =&gt; 2 x,1
</span><span class="comment">-- =&gt; 3 z,3</span>
</pre>


<h3>toObj (kvpairs)</h3>

<p>Converts an array list of <a href="index.html#kvpairs">kvpairs</a> to an object where keys are taken from the 1rst column in the <a href="index.html#kvpairs">kvpairs</a> sequence, associated with values in the 2nd column.</p>


<pre>
<span class="keyword">local</span> list_pairs = {{<span class="string">'x'</span>,<span class="number">1</span>},{<span class="string">'y'</span>,<span class="number">2</span>},{<span class="string">'z'</span>,<span class="number">3</span>}}
obj = M.toObj(list_pairs)

<span class="comment">-- =&gt; {x = 1, y = 2, z = 3}</span>
</pre>


<h3>invert (obj)</h3>
<p>*Aliases: <code>mirror</code>*.</p>

<p>Switches <tt>key-value</tt> pairs:</p>


<pre>
M.invert {<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>} <span class="comment">-- =&gt; "{a=1, b=2, c=3}"
</span>M.invert {x = <span class="number">1</span>, y = <span class="number">2</span>} <span class="comment">-- =&gt; "{'x','y'}"</span>
</pre>


<h3>property (key)</h3>

<p>Returns a function that will return the key property of any passed-in object.</p>


<pre>
<span class="keyword">local</span> who = M.property(<span class="string">'name'</span>)
<span class="keyword">local</span> people = {name = <span class="string">'Henry'</span>}
who(people) <span class="comment">-- =&gt; 'Henry'</span>
</pre>


<h3>propertyOf (obj)</h3>

<p>Returns a function that will return the key property of any passed-in object.</p>


<pre>
<span class="keyword">local</span> people = {name = <span class="string">'Henry'</span>}
<span class="global">print</span>(M.propertyOf(people)(<span class="string">'name'</span>)) <span class="comment">-- =&gt; 'Henry'</span>
</pre>


<h3>toBoolean (value)</h3>

<p>Converts a given value to a boolean.</p>


<pre>
M.toBoolean(<span class="keyword">true</span>) <span class="comment">-- =&gt; true
</span>M.toBoolean(<span class="keyword">false</span>) <span class="comment">-- =&gt; false
</span>M.toBoolean(<span class="keyword">nil</span>) <span class="comment">-- =&gt; false
</span>M.toBoolean({}) <span class="comment">-- =&gt; true
</span>M.toBoolean(<span class="number">1</span>) <span class="comment">-- =&gt; true</span>
</pre>


<h3>extend (destObj, ...)</h3>

<p>Extends a destination object with the properties of some source objects.</p>


<pre>
M.extend({},{a = <span class="string">'b'</span>, c = <span class="string">'d'</span>}) <span class="comment">-- =&gt; "{a = 'b', c = 'd'}"</span>
</pre>


<h3>functions (obj [, recurseMt])</h3>
<p>*Aliases: <code>methods</code>*.</p>

<p>Returns all functions names within an object.</p>


<pre>
M.functions(<span class="global">coroutine</span>)
<span class="comment">-- =&gt; "{'yield','wrap','status','resume','running','create'}"</span>
</pre>


<p>When given <code>recurseMt</code>, will also include <code>obj</code> metatable's functions.</p>


<pre>
<span class="keyword">local</span> mt = {<span class="global">print</span> = <span class="global">print</span>}
<span class="keyword">local</span> t = {<span class="global">assert</span> = <span class="global">assert</span>}
<span class="global">setmetatable</span>(t, {__index = mt})
M.functions(t, <span class="keyword">true</span>) <span class="comment">-- =&gt; "{'assert','print'}"</span>
</pre>


<h3>clone (obj [, shallow])</h3>

<p>Clones a given object.</p>


<pre>
<span class="keyword">local</span> obj = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}
<span class="keyword">local</span> obj2 = M.clone(obj)
<span class="global">print</span>(obj2 == obj) <span class="comment">-- =&gt; false
</span><span class="global">print</span>(M.isEqual(obj2, obj)) <span class="comment">-- =&gt; true</span>
</pre>


<h3>tap (obj, f)</h3>

<p>Invokes a given interceptor function on some object, and then returns the object itself. Useful to tap into method chaining to hook intermediate results.
The passed-in interceptor should be prototyped as <code>f(obj,...)</code>.</p>


<pre>
<span class="keyword">local</span> v = M.chain({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>})
  :filter(<span class="keyword">function</span>(v) <span class="keyword">return</span> v%<span class="number">2</span>~=<span class="number">0</span> <span class="keyword">end</span>) <span class="comment">-- retain odd values
</span>  :tap(<span class="keyword">function</span>(v) <span class="global">print</span>(<span class="string">'Max is'</span>, M.max(v) <span class="keyword">end</span>) <span class="comment">-- Tap max value
</span>  :map(<span class="keyword">function</span>(v) <span class="keyword">return</span> v^<span class="number">2</span> <span class="keyword">end</span>)
  :value() <span class="comment">-- =&gt;    Max is 89</span>
</pre>


<h3>has (obj, key)</h3>

<p>Checks if an object has a given attribute.</p>


<pre>
M.has(_,<span class="string">'has'</span>) <span class="comment">-- =&gt; true
</span>M.has(<span class="global">coroutine</span>,<span class="string">'resume'</span>) <span class="comment">-- =&gt; true
</span>M.has(<span class="global">math</span>,<span class="string">'random'</span>) <span class="comment">-- =&gt; true</span>
</pre>


<h3>pick (obj, ...)</h3>
<p>*Aliases: <code>choose</code>*.</p>

<p>Collects whilelisted properties of a given object.</p>


<pre>
<span class="keyword">local</span> object = {a = <span class="number">1</span>, b = <span class="number">2</span>, c = <span class="number">3</span>}
M.pick(object,<span class="string">'a'</span>,<span class="string">'c'</span>) <span class="comment">-- =&gt; "{a = 1, c = 3}"</span>
</pre>


<h3>omit (obj, ...)</h3>
<p>*Aliases: <code>drop</code>*.</p>

<p>Omits blacklisted properties of a given object.</p>


<pre>
<span class="keyword">local</span> object = {a = <span class="number">1</span>, b = <span class="number">2</span>, c = <span class="number">3</span>}
M.omit(object,<span class="string">'a'</span>,<span class="string">'c'</span>) <span class="comment">-- =&gt; "{b = 2}"</span>
</pre>


<h3>template (obj [, template])</h3>
<p>*Aliases: <code>defaults</code>*.</p>

<p>Applies a template on an object, preserving existing properties.</p>


<pre>
<span class="keyword">local</span> obj = {a = <span class="number">0</span>}
M.template(obj,{a = <span class="number">1</span>, b = <span class="number">2</span>, c = <span class="number">3</span>}) <span class="comment">-- =&gt; "{a=0, c=3, b=2}"</span>
</pre>


<h3>isEqual (objA, objB [, useMt])</h3>
<p>*Aliases: <code>compare</code>, <code>M.matches</code>*.</p>

<p>Compares objects:</p>


<pre>
M.isEqual(<span class="number">1</span>,<span class="number">1</span>) <span class="comment">-- =&gt; true
</span>M.isEqual(<span class="keyword">true</span>,<span class="keyword">false</span>) <span class="comment">-- =&gt; false
</span>M.isEqual(<span class="number">3.14</span>,<span class="global">math</span>.pi) <span class="comment">-- =&gt; false
</span>M.isEqual({<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>},{<span class="number">3</span>,<span class="number">4</span>,{<span class="number">5</span>}}) <span class="comment">-- =&gt; false</span>
</pre>


<h3>result (obj, method)</h3>

<p>Calls an object method, passing it as a first argument the object itself.</p>


<pre>
M.result(<span class="string">'abc'</span>,<span class="string">'len'</span>) <span class="comment">-- =&gt; 3
</span>M.result({<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>},<span class="global">table</span>.concat) <span class="comment">-- =&gt; 'abc'</span>
</pre>


<h3>isTable (t)</h3>

<p>Is the given argument an object (i.e a table) ?</p>


<pre>
M.isTable({}) <span class="comment">-- =&gt; true
</span>M.isTable(<span class="global">math</span>) <span class="comment">-- =&gt; true
</span>M.isTable(<span class="global">string</span>) <span class="comment">-- =&gt; true</span>
</pre>


<h3>isCallable (obj)</h3>

<p>Is the given argument callable ?</p>


<pre>
M.isCallable(<span class="global">print</span>) <span class="comment">-- =&gt; true
</span>M.isCallable(<span class="keyword">function</span>() <span class="keyword">end</span>) <span class="comment">-- =&gt; true
</span>M.isCallable(<span class="global">setmetatable</span>({},{__index = <span class="global">string</span>}).upper) <span class="comment">-- =&gt; true
</span>M.isCallable(<span class="global">setmetatable</span>({},{__call = <span class="keyword">function</span>() <span class="keyword">return</span> <span class="keyword">end</span>})) <span class="comment">-- =&gt; true</span>
</pre>


<h3>isArray (obj)</h3>

<p>Is the given argument an array (i.e. a sequence) ?</p>


<pre>
M.isArray({}) <span class="comment">-- =&gt; true
</span>M.isArray({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}) <span class="comment">-- =&gt; true
</span>M.isArray({<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>}) <span class="comment">-- =&gt; true</span>
</pre>


<h3>isIterable (obj)</h3>

<p>Checks if the given object is iterable with <a href="https://www.lua.org/manual/5.1/manual.html#pdf-pairs">pairs</a>.</p>


<pre>
M.isIterable({}) <span class="comment">-- =&gt; true
</span>M.isIterable(<span class="keyword">function</span>() <span class="keyword">end</span>) <span class="comment">-- =&gt; false
</span>M.isIterable(<span class="keyword">false</span>) <span class="comment">-- =&gt; false
</span>M.isIterable(<span class="number">1</span>) <span class="comment">-- =&gt; false</span>
</pre>


<h3>type (obj)</h3>

<p>Extends Lua's <a href="index.html#type">type</a> function. It returns the type of the given object and also recognises 'file' userdata</p>


<pre>
M.<span class="global">type</span>(<span class="string">'string'</span>) <span class="comment">-- =&gt; 'string'
</span>M.<span class="global">type</span>(<span class="global">table</span>) <span class="comment">-- =&gt; 'table'
</span>M.<span class="global">type</span>(<span class="keyword">function</span>() <span class="keyword">end</span>) <span class="comment">-- =&gt; 'function'
</span>M.<span class="global">type</span>(<span class="global">io</span>.open(<span class="string">'f'</span>,<span class="string">'w'</span>)) <span class="comment">-- =&gt; 'file'</span>
</pre>


<h3>isEmpty ([obj])</h3>

<p>Is the given argument empty ?</p>


<pre>
M.isEmpty(<span class="string">''</span>) <span class="comment">-- =&gt; true
</span>M.isEmpty({})  <span class="comment">-- =&gt; true
</span>M.isEmpty({<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>}) <span class="comment">-- =&gt; false</span>
</pre>


<h3>isString (obj)</h3>

<p>Is the given argument a string ?</p>


<pre>
M.isString(<span class="string">''</span>) <span class="comment">-- =&gt; true
</span>M.isString(<span class="string">'Hello'</span>) <span class="comment">-- =&gt; false
</span>M.isString({}) <span class="comment">-- =&gt; false</span>
</pre>


<h3>isFunction (obj)</h3>

<p>Is the given argument a function ?</p>


<pre>
M.isFunction(<span class="global">print</span>) <span class="comment">-- =&gt; true
</span>M.isFunction(<span class="keyword">function</span>() <span class="keyword">end</span>) <span class="comment">-- =&gt; true
</span>M.isFunction({}) <span class="comment">-- =&gt; false</span>
</pre>


<h3>isNil (obj)</h3>

<p>Is the given argument nil ?</p>


<pre>
M.isNil(<span class="keyword">nil</span>) <span class="comment">-- =&gt; true
</span>M.isNil() <span class="comment">-- =&gt; true
</span>M.isNil({}) <span class="comment">-- =&gt; false</span>
</pre>


<h3>isNumber (obj)</h3>

<p>Is the given argument a number ?</p>


<pre>
M.isNumber(<span class="global">math</span>.pi) <span class="comment">-- =&gt; true
</span>M.isNumber(<span class="global">math</span>.huge) <span class="comment">-- =&gt; true
</span>M.isNumber(<span class="number">0</span>/<span class="number">0</span>) <span class="comment">-- =&gt; true
</span>M.isNumber() <span class="comment">-- =&gt; false</span>
</pre>


<h3>isNaN (obj)</h3>

<p>Is the given argument NaN ?</p>


<pre>
M.isNaN(<span class="number">1</span>) <span class="comment">-- =&gt; false
</span>M.isNaN(<span class="number">0</span>/<span class="number">0</span>) <span class="comment">-- =&gt; true</span>
</pre>


<h3>isFinite (obj)</h3>

<p>Is the given argument a finite number ?</p>


<pre>
M.isFinite(<span class="number">99e99</span>) <span class="comment">-- =&gt; true
</span>M.isFinite(<span class="global">math</span>.pi) <span class="comment">-- =&gt; true
</span>M.isFinite(<span class="global">math</span>.huge) <span class="comment">-- =&gt; false
</span>M.isFinite(<span class="number">1</span>/<span class="number">0</span>) <span class="comment">-- =&gt; false
</span>M.isFinite(<span class="number">0</span>/<span class="number">0</span>) <span class="comment">-- =&gt; false</span>
</pre>


<h3>isBoolean (obj)</h3>

<p>Is the given argument a boolean ?</p>


<pre>
M.isBoolean(<span class="keyword">true</span>) <span class="comment">-- =&gt; true
</span>M.isBoolean(<span class="keyword">false</span>) <span class="comment">-- =&gt; true
</span>M.isBoolean(<span class="number">1</span>==<span class="number">1</span>) <span class="comment">-- =&gt; true
</span>M.isBoolean(<span class="global">print</span>) <span class="comment">-- =&gt; false</span>
</pre>


<h3>isInteger (obj)</h3>

<p>Is the given argument an integer ?</p>


<pre>
M.isInteger(<span class="global">math</span>.pi) <span class="comment">-- =&gt; false
</span>M.isInteger(<span class="number">1</span>) <span class="comment">-- =&gt; true
</span>M.isInteger(-<span class="number">1</span>) <span class="comment">-- =&gt; true</span>
</pre>


<p><strong><a href="#TOC">[⬆]</a></strong></p>

<p><a name="_a_name__chaining__Chaining__a_"></a></p>
<h2><a name='chaining'>Chaining</a></h2>

<p><em>Method chaining</em> (also known as <em>name parameter idiom</em>), is a technique for invoking consecutively method calls in object-oriented style.
Each method returns an object, and method calls are chained together.
Moses offers chaining for your perusal. <br/></p>

<p>Let's use chaining to get the count of evey single word in some lyrics (case won't matter here).</p>



<pre>
<span class="keyword">local</span> lyrics = {
  <span class="string">"I am a lumberjack and I am okay"</span>,
  <span class="string">"I sleep all night and I work all day"</span>,
  <span class="string">"He is a lumberjack and he is okay"</span>,
  <span class="string">"He sleeps all night and he works all day"</span>
}

<span class="comment">-- split a text into words
</span><span class="keyword">local</span> <span class="keyword">function</span> words(line)
  <span class="keyword">local</span> t = {}
  <span class="keyword">for</span> w <span class="keyword">in</span> line:gmatch(<span class="string">'(%w+)'</span>) <span class="keyword">do</span> t[#t+<span class="number">1</span>] = w <span class="keyword">end</span>
  <span class="keyword">return</span> t
<span class="keyword">end</span>

<span class="keyword">local</span> stats = M.chain(lyrics)
  :map(words)
  :flatten()
  :countBy(<span class="global">string</span>.lower)
  :value()

<span class="comment">-- =&gt; "{
</span><span class="comment">-- =&gt;    sleep = 1, night = 2, works = 1, am = 2, is = 2,
</span><span class="comment">-- =&gt;    he = 2, and = 4, I = 4, he = 2, day = 2, a = 2,
</span><span class="comment">-- =&gt;    work = 1, all = 4, okay = 2
</span><span class="comment">-- =&gt;  }"</span>
</pre>


<p>For convenience, you can also use <code>M(value)</code> to start chaining methods, instead of <code>M.chain(value)</code>.</p>

<p>Note that one can use <code>:value()</code> to unwrap a chained object.</p>


<pre>
<span class="keyword">local</span> t = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>}
<span class="global">print</span>(_(t):value() == t) <span class="comment">-- =&gt; true</span>
</pre>


<p><strong><a href="#TOC">[⬆]</a></strong></p>

<p><a name="_a_name__import__Import__a_"></a></p>
<h2><a name='import'>Import</a></h2>

<p>All library functions can be imported in a context using <a href="index.html#import">import</a> into a specified context.</p>


<pre>
<span class="keyword">local</span> context = {}
M.import(context)

context.each({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>},<span class="global">print</span>)

<span class="comment">-- =&gt; 1 1
</span><span class="comment">-- =&gt; 2 2
</span><span class="comment">-- =&gt; 3 3</span>
</pre>


<p>When no <code>context</code> was provided, it defaults to the current environment, <code>_ENV</code> or <code>_G</code>.</p>


<pre>
M.import()

each({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>},<span class="global">print</span>)

<span class="comment">-- =&gt; 1 1
</span><span class="comment">-- =&gt; 2 2
</span><span class="comment">-- =&gt; 3 3</span>
</pre>


<p>Passing <code>noConflict</code> argument leaves untouched conflicting keys while importing into the context.</p>


<pre>
<span class="keyword">local</span> context = {each = <span class="number">1</span>}
M.import(context, <span class="keyword">true</span>)

<span class="global">print</span>(context.each) <span class="comment">-- =&gt; 1
</span>context.eachi({<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>},<span class="global">print</span>)

<span class="comment">-- =&gt; 1 1
</span><span class="comment">-- =&gt; 2 2
</span><span class="comment">-- =&gt; 3 3</span>
</pre>


<p><strong><a href="#TOC">[⬆]</a></strong>

</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2019-04-01 23:55:17 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>
